#include<iostream>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#define h(x) hsh[x]
using namespace std;
const int N=2e5+10;
const int INF=2e9;
map<string,int>hsh;
queue<string>q[101];
int sp[101];
int inq[101];
inline bool ck(string f,string s){
int ff=f.size();
int ss=s.size();
if(ss<ff) return 0;
for(int i=0;i<min(ff,ss);i++)if(f[i]!=s[i])return 0;
return 1;
}
int tot=1;
int main(){
string t,s="";
int n;
q[0].push("");
hsh[s]=0;
cin>>t>>n;
for(int i=0;i<n;i++){
// cout<<q[i].size()<<endl;
memset(inq,0,sizeof(inq));
int a;
cin>>a;
for(int j=0;j<a;j++){
string gg;
cin>>gg;
for(int k=0;k<q[i].size();k++){
string er=q[i].front();q[i].pop();q[i].push(er);
string tmp=er;
string tmp2=er;
tmp.append(gg);
if(ck(tmp,t)){
if(h(tmp)==0){
h(tmp)=tot++;
sp[h(tmp)]=sp[h(tmp2)]+1;
// cout<<tmp<<" "<<tmp2<<" "<<sp[h(tmp)]<<endl;
}
if(!inq[h(tmp)])q[i+1].push(tmp),inq[h(tmp)]=1;
}
}
}
string gg="";
string er=q[i].front();q[i].pop();
string tmp=er;
string tmp2=er;
tmp.append(gg);
if(ck(tmp,t)){
if(h(tmp)==0){
h(tmp)=tot++;
sp[h(tmp)]=sp[h(tmp2)]+1;
// cout<<tmp<<" "<<tmp2<<" "<<sp[h(tmp)]<<endl;
}
if(!inq[h(tmp)])q[i+1].push(tmp),inq[h(tmp)]=1;
}
}
if(sp[h(t)]==0)cout<<-1<<endl;
else cout<<sp[h(t)]<<endl;
return 0;
}
然而 WA 了 9 个测试点,你们看看我错在哪了?
思路:枚举每一个”上一次“的情况,在后面尝试加上当前字符串,如果与原串匹配就入队