Neal is very curious about combinatorial problems, and now here comes a problem about words. Know- ing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie.
Since Jiejie can’t remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie’s only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks.
The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the words in the set.
Input
The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000.
The second line contains an integer S, 1 ≤ S ≤ 4000.
Each of the following S lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase.
There is a blank line between consecutive test cases. You should proceed to the end of file.
Output
For each test case, output the number, as described above, from the task description modulo 20071027.
Sample Input
abcd
4
a
b
cd
ab
Sample Output
Case 1: 2
题意:给一个长串,再给n个小串,问长串可以由小串组成的方式有多少种,小串可以重复利用
题解:可以直接用字典树解决的问题,但是还是用它来练一练AC自动机吧。(才学AC自动机,代码有点丑,勿喷)
dp[i] 表示长度为i的前缀的组合方式种类数,dp[i] += dp[i-len],s[i-len] 到 s[i] 为一个完整的小串。
dp的思想很简单,但是字符串需要AC自动机来处理。
附上AC代码:
//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node<<1
#define rson mid + 1, r, node<<1|1
#define Root 1, m, 1
#define Node l, r, node
const int INF = 0x3f3f3f3f;
const int O = 1e6;
const int mod = 20071027;
const int maxn = 5e5 + 5;
const double PI = acos(-1.0);
const double E = 2.718281828459;
//AC自动机模板有last优化
struct AC_automation{
int trie[maxn][26];
int valori[maxn], val[maxn], fail[maxn], depth[maxn], last[maxn];
int cnt, M = 26;
AC_automation(){ init(); }
void init() {
cnt = 0;
for(int i=0; i<maxn; i++){
MT(trie[i], 0);
valori[i] = fail[i] = depth[i] = last[i] = 0;
}
}
void insert(char * s){
int now = 0;
for(int i=0; s[i]; i++) {
int c = s[i] - 'a';
if(!trie[now][c]) trie[now][c] = ++ cnt;
now = trie[now][c];
}
valori[now] ++;
depth[now] = (int)strlen(s);
}
void build(){
queue<int>q;
for(int i=0; i<M; i++) if(trie[0][i] ) q.push(trie[0][i]);
while(!q.empty()){
int now = q.front(); q.pop();
for(int i=0; i<M; i++) {
int & son = trie[now][i];
if(son) {
fail[son] = trie[fail[now]][i];
last[son] = valori[fail[son]] ? fail[trie[now][i]] : last[fail[son]];;
q.push(son);
}
else son = trie[fail[now]][i];
}
}
}
// int match(char * s){
// memcpy(val, valori, sizeof(valori));
// int p = 0, ans = 0;
// for(int i=0; s[i]; i++) {
// p = trie[p][s[i] - 'a'];
// for(int j=p; j && ~val[j]; j=last[j]) {
// ans += val[j]; val[j] = -1;
// }
// }
// return ans;
// }
}AC;
void query(char * s, int * dp) {
int root = 0;
for(int i=0; s[i]; i++) {
int c = s[i] - 'a';
root = AC.trie[root][c];
int now = root;
while(now != 0){
if(AC.valori[now] != 0) dp[i+1] = (dp[i+1] + dp[i + 1 - AC.depth[now]]) % mod;
now = AC.last[now];
}
}
}
int main(){
int l = 0, dp[maxn]; char s[maxn];
while( ~scanf("%s", s)){
AC.init();
int n; scanf("%d", &n);
while( n -- ) {
char ss[maxn]; scanf("%s", ss);
AC.insert(ss);
}
AC.build();
MT(dp, 0); dp[0] = 1;
query(s, dp);
printf("Case %d: %d\n", ++l, dp[strlen(s)]);
}
return 0;
}