E. Masha-forgetful
题链
结论就是任何长的串都可以被2,3长度的串表示
后面就是暴力hash和很常规的dp[i]表示前i个是否匹配了
void solve(){
int n,m;cin>>n>>m;
tuple<int,int,int>f2[10][10],f3[10][10][10];
for(int i=1;i<=n;i++){
string s;cin>>s;
for(int j=0;j<m;j++){
if(j+1<m){
f2[s[j]-'0'][s[j+1]-'0']={j,j+1,i};
}
if(j+2<m){
f3[s[j]-'0'][s[j+1]-'0'][s[j+2]-'0']={j,j+2,i};
}
}
}
vector<bool>dp(m+1);
dp[0]=1;
tuple<int,int,int>k={0,0,0};
string s;cin>>s;
for(int i=0;i<m;i++){
if (!dp[i]) {
continue;
}
if (i + 2 <= m && f2[s[i] - '0'][s[i + 1] - '0'] != k) {
dp[i + 2] = true;
}
if (i + 3 <= m && f3[s[i] - '0'][s[i + 1] - '0'][s[i + 2] - '0'] != k) {
dp[i + 3] = true;
}
}
if(!dp[m]){
cout<<-1<<endl;
}else{
int i=m;
vector<tuple<int,int,int>>ans;
while(i){
if(i>=2&&dp[i-2]&&f2[s[i-2]-'0'][s[i-1]-'0']!=k){
ans.push_back(f2[s[i-2]-'0'][s[i-1]-'0']);
i-=2;
}else{
ans.push_back(f3[s[i-3]-'0'][s[i-2]-'0'][s[i-1]-'0']);
i-=3;
}
}
reverse(ans.begin(), ans.end());
cout<<ans.size()<<endl;
for(auto [i,j,k]:ans){
cout<<i+1<<' '<<j+1<<' '<<k<<endl;
}
}
}
标签:10,f2,f3,int,Codeforces,764,ans,Round,dp
From: https://www.cnblogs.com/ycllz/p/17013366.html