第一题:
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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 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 , 1S4000 .
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
#include
#include
#include
using namespace std;
#define maxn 400000 + 10
#define M 300000 + 10
#define mod 20071027
int tree[maxn][27], val[maxn], top;
int d[maxn];
char s[M], tmp[105];
void init()
{
top = 1;
memset(tree[0], 0, sizeof tree[0]);
memset(d, 0, sizeof d);
}
void insert(char *s, int v)
{
int len = strlen(s), u = 0;
for(int i = 0; i < len; i++)
{
int c = s[i] - 'a' ;
if(!tree[u][c])
{
memset(tree[top], 0, sizeof tree[top]);
val[top] = 0;
tree[u][c] = top++;
}
u = tree[u][c];
}
val[u] = v;
}
int len ;
int query(char *s, int pos)
{
int u = 0;
int res = 0;
for(int i = pos; i < len; i++)
{
int c = s[i] - 'a' ;
u = tree[u][c];
if(!u) return res;
if(val[u])
{
res = (res + d[i + 1] ) % mod ;
}
}
return res;
}
int main()
{
int kase = 0;
while(~scanf("%s", s))
{
init();
len = strlen(s);
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%s", tmp);
insert(tmp, 1);
}
d[len] = 1;
for(int i = len - 1; i >= 0; i--)
{
d[i] = query(s, i);
}
printf("Case %d: %d\n", ++kase, d[0]);
}
return 0;
}
#include
#include
#include
#include
#define MOD 998244353
#define M 2000010
using namespace std;
int top;// top 表示节点个数
int tree[M][2]; // 字典树
int val[M]; //字符串的权值 当val[i] 大于0时i是单词节点
void init()
{
top=1;
//memset(tree, 0, sizeof tree);
memset(tree[0], 0, sizeof tree[0]);
memset(val, 0, sizeof val);
}
void insert(int v)
{
int u = 0;//根节点
for(int i = 0; i < 30 ; i++)
{
int t = v & ( 1<< i );
if(t) t = 1;
if(!tree[u][t])
{
memset(tree[top], 0, sizeof tree[top]);
val[top] = 1;
tree[u][t] = top++;
}
else
{
val[tree[u][t]]++;
}
u = tree[u][t];
}
}
int ans;
void find(int v)
{
int u = 0;
for(int i = 0; i < 30; i++)
{
int t = v & (1 << i);
if(t) t = 1;
if(tree[u][t ^ 1])
ans = ( ans + (val[tree[u][t ^ 1]]) * (1 << i)) % MOD;
u = tree[u][t] ;
}
}
int a[M];
int main()
{
int T, n;
scanf("%d", &T);
int kase = 0;
while(T--)
{
init();
ans = 0;
scanf("%d", &n);
for(int i=0; i