@(ACM题目)[字符串, Trie树]
Description
Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing 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 alculate 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
题目分析
本次给定一字符串
s
,以及一个字典(字符串的set),问将字符串拆成字典中的单词(不能相交)有多少种拆法。
本题属于基础的Trie树,利用Trie树优化dp。
设
如果brute force的话,这个 dp 是 O(n⋅l2S) 的( l 为字典中单词的长度),当然你也可以用二分优化查询,不过也会TLE。
用Trie优化“查询
UPD:优化的解法
代码
#include<bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
const int maxn = 3e5+5;
const int maxn2 = 400000+8;
const int maxm = 26;
const int maxn3 = 105;
const int M = 20071027;
char s[maxn], s2[maxn3];
struct Trie
{
int ch[maxn2][maxm];
bitset<maxn2> val;
int tot = 1;
void init()
{
memset(ch[0], 0, sizeof ch[0]);
val.reset();
tot = 1;
}
void add(char *s)
{
int n = strlen(s);
int cur = 0, id;
for(int i = 0; i < n; ++i)
{
id = s[i] - 'a';
if(!ch[cur][id])
{
memset(ch[tot], 0, sizeof ch[tot]);
ch[cur][id] = tot++;
}
cur = ch[cur][id];
}
val[cur] = 1;
}
bool finda(char *s)
{
int n = strlen(s);
int cur = 0, id;
for(int i = 0; i < n; ++i)
{
id = s[i] - 'a';
if(!ch[cur][id]) return false;
cur = ch[cur][id];
}
return val.test(cur);
}
}tr;
LL dp[maxn];
int main()
{
int Case = 1;
while(~scanf("%s", s+1))
{
int n;
cin >> n;
int len = strlen(s+1);
tr.init();
int mxLen = -1;
for(int i = 0; i < n; ++i)
{
scanf("%s", s2);
mxLen = max(mxLen, (int)strlen(s2));
tr.add(s2);
}
memset(dp, 0, sizeof dp);
dp[0] = 1;
memset(s2, 0, sizeof s2);
for(int i = 1; i <= len; ++i)
{
int bound = max(1, i - mxLen + 1);
int cnt = 0;
for(int j = i; j >= bound; --j)
{
for(int k = j; k <= i; ++k) s2[k - j] = s[k];
s2[i - j + 1] = '\0';
if(tr.finda(s2))
dp[i] = (dp[i] + dp[j-1]) % M;
}
}
printf("Case %d: %d\n", Case++, dp[len]);
}
return 0;
}