UVAL 3942 Remember the Word(递推+Trie) Remember the Word

【题目链接】Remember the Word

【题目类型】递推+Trie

&题解:

蓝书P209,参考的别人公开代码

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn= 4005*100 +7,sigmaSize=26;
int ch[maxn][sigmaSize],sz,K;
bool vis[maxn];
void init() {sz=1; memset(ch[0],0,sizeof(ch[0]));}
void add(char *s) {
	int u=0;
	for(int i=0; s[i]; i++) {
		int c=s[i]-'a';
		if(!ch[u][c]) {
			memset(ch[sz],0,sizeof(ch[sz]));
			vis[sz]=false;
			ch[u][c]=sz++;
		}
		u=ch[u][c];
	}
	vis[u]=true;
}
const int LEN=3e5+5,mod=20071027;
char str[LEN];
int d[LEN];
int main() {
	freopen("E:1.in","r",stdin);
	char word[120];
	while(~scanf("%s",str)) {
		init();
		int S; scanf("%d",&S);
		while(S--) {
			scanf("%s",word);
			add(word);
		}
		int n=0;
		while(str[n]) d[n++]=-1;
		d[n]=1;
		for(int cur=n-1; cur>=0; cur--) {
			int u=0;
			d[cur]=0;
			for(int i=cur; i<n; i++) {
				int c=str[i]-'a';
				if(!ch[u][c]) break;
				u=ch[u][c];
				if(vis[u]) d[cur]=(d[cur]+d[i+1])%mod;
			}
		}
		printf("Case %d: %d
",++K,d[0]);
	}
	return 0;
}