1624E - Masha-forgetful - CodeForces Solution


brute force constructive algorithms dp hashing implementation strings *2000

Please click on ads to support us..

Python Code:

import io, os

t = int(input())
for tidx in range(t):
    input()
    n, m = [int(x) for x in input().split()]
    d = dict()
    for i in range(n):
        st = input()
        for j in range(m-1):
            d[st[j:(j+2)]] = (j, j+1, i)
        for j in range(m-2):
            d[st[j:(j+3)]] = (j, j+2, i)
    st = input()
    segments = [(0,0,0)]*m
    bools = [False]*m
    for i in range(m):
        if i==1 or (i>1 and bools[i-2]):
            if st[(i-1):(i+1)] in d:
                segments[i] = d[st[(i-1):(i+1)]]
                bools[i] = True
        if i==2 or (i>2 and bools[i-3]):
            if st[(i-2):(i+1)] in d:
                segments[i] = d[st[(i-2):(i+1)]]
                bools[i] = True
    
    if bools[m-1]:
        out = []
        idx = m-1
        while idx>0:
            out.append(segments[idx])
            idx -= 1+segments[idx][1]-segments[idx][0]
        k = len(out)
        print(k)
        for i in range(k-1,-1,-1):
            print(out[i][0]+1, out[i][1]+1, out[i][2]+1)
    else: print(-1)

C++ Code:

#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#include <random>
#include<algorithm>
using namespace std;
using int64 = long long;
using uint = unsigned int;

void solve(){
    int n, m;
    cin>>n>>m;
    unordered_map<string, vector<int>> mp;
    mp.rehash(2 * n);
    string s;
    for(int i = 0; i < n; i++){
        cin>>s;
        for(int j = 0; j + 1 < m; j++){
            string tmp = s.substr(j, 2);
            mp[tmp] = {j + 1, j + 2, i + 1};
            if(j + 2 < m){
                tmp += s[j + 2];
                mp[tmp] = {j + 1, j + 3, i + 1};
            }
        }
    }
    cin>>s;
    vector<int> dp(m + 1, 0);
    dp[0] = 1;
    for(int i = 0; i < m - 1; ++i){
        if(!dp[i]) continue;
        string tmp = s.substr(i, 2);
        if(mp.count(tmp)) dp[i + 2] = 1;
        if(i < m - 2) {
            tmp += s[i + 2];
            if(mp.count(tmp)) dp[i + 3] = 1;
        }
    }
    if(dp[m]){
        vector<vector<int>> res;
        int l = m;
        while(l > 0){
            if(dp[l - 2]){
                string tmp = s.substr(l - 2, 2);
                res.push_back(mp[tmp]);
                l -= 2;
            }
            else{
                string tmp = s.substr(l - 3, 3);
                res.push_back(mp[tmp]);
                l -= 3;
            }
        }
        cout<<res.size()<<'\n';
        for(int i = res.size() - 1; i >= 0; --i){
             cout<<res[i][0]<<' '<<res[i][1]<<' '<<res[i][2]<<'\n';
        }
    }
    else{
        cout<<-1<<'\n';
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    // int t = 1;
    int t;
    cin>>t;
    
    while(t--){
        solve();
    }
    return 0;
} 


Comments

Submit
0 Comments
More Questions

553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating