标签:style blog http 使用 os io strong for
#include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<cstdio> #include<vector> #include<string> #include<fstream> #include<cstring> #include<ctype.h> #include<iostream> #include<algorithm> #define INF (1<<30) #define PI acos(-1.0) #define mem(a, b) memset(a, b, sizeof(a)) #define rep(i, n) for (int i = 0; i < n; i++) #define debug puts("===============") typedef long long ll; using namespace std; const int maxnode = 400010, charset = 26; int d[300100] = {0}, len; const int mod = 20071027; struct Trie { int ch[maxnode][charset]; int val[maxnode]; int sz; void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); } int idx(char c) { return c - 'a'; } void insert(char *s, int v) { int u = 0, n = strlen(s); for (int i = 0; i < n; i++) { int c = idx(s[i]); if (!ch[u][c]) { memset(ch[sz], 0, sizeof(ch[sz])); val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } val[u] = v; } void query(char *s, int x) { int u = 0, ans = 0; for (int i = x; i < len; i++) { int c = idx(s[i]); if (!ch[u][c]) return ; u = ch[u][c]; if (val[u]) d[x] = (d[x] + d[i + 1]) % mod; } } }; char w[110], str[300100]; Trie T; int main () { int cnt = 1, n; while(~scanf("%s", str)) { len = strlen(str); scanf("%d", &n); T.init(); for(int i = 0; i < n; i++) { scanf("%s", w); T.insert(w, 1); } d[len] = 1; for (int i = len - 1; i >= 0; i--) { d[i] = 0; T.query(str, i); } printf("Case %d: %d\n", cnt++, d[0]); } return 0; }
LA 3942 Remember the Word (Trie),布布扣,bubuko.com
LA 3942 Remember the Word (Trie)
标签:style blog http 使用 os io strong for
原文地址:http://blog.csdn.net/sio__five/article/details/38520389