题意:
给你一个长度为n的字符串S,和m个字符串T,询问能不能用这些字符串T将他们倒序,然后组成S
题解:
将这些m个字符串T,倒序放入Trie树中,然后用S在Trie树上匹配就好了
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+50;
const int MAXM = 1e6+50;
int nxt[MAXM][26],id[MAXM],n,m,tot;
char s[MAXN],str[MAXN][1050];
int ans,v[MAXM],cnt;
bool ok = false;
inline void Insert(int k){
int root=0,len=strlen(str[k]),x;
for(int i=len-1;i>=0;i--){
if(str[k][i]>='A' && str[k][i]<='Z') x = str[k][i]-'A';
else x = str[k][i]-'a';
if(!nxt[root][x]) nxt[root][x] = ++tot;
root = nxt[root][x];
}
id[root] = k;
}
void dfs(int now,int len){
if(len==n) { ok=true;cnt=now;return; }
int root=0;
for(int i=len;i<n;i++){
if(!nxt[root][s[i]-'a']) return;
if(id[nxt[root][s[i]-'a']]){
v[now]=id[nxt[root][s[i]-'a']];
dfs(now+1,i+1);
if(ok) return;
}
root=nxt[root][s[i]-'a'];
}
}
int main(){
scanf("%d%s%d",&n,s,&m);
for(int i=1;i<=m;i++) scanf("%s",str[i]),Insert(i);
dfs(0,0);
for(int i=0;i<cnt;i++) printf("%s ",str[v[i]]); puts("");
return 0;
}