UVA 1401 Remember the Word
Remember the Word
Time Limit: 3000MS
Memory Limit: Unknown
64bit IO Format: %lld & %llu
[Submit] [Go Back] [Status]
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
Each of the following S
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
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <string>
5
6 using namespace std;
7
8 const int MOD=20071027;
9 const int maxn=400010;
10
11 int m,dp[400030];
12 char str[400030];
13
14 struct Trie
15 {
16 int tot,root,child[maxn][27];
17 bool flag[maxn];
18 Trie()
19 {
20 memset(child[1],0,sizeof(child[1]));
21 flag[1]=false;
22 root=tot=1;
23 }
24 void Init()
25 {
26 memset(child[1],0,sizeof(child[1]));
27 flag[1]=false;
28 root=tot=1;
29 }
30 void Insert(const char*str)
31 {
32 int *cur=&root;
33 for(const char *p=str;*p;p++)
34 {
35 cur=&child[*cur][*p-'a'];
36 if(*cur==0)
37 {
38 *cur=++tot;
39 memset(child[tot],0,sizeof(child[tot]));
40 flag[tot]=false;
41 }
42 }
43 flag[*cur]=true;
44 }
45 bool query(const char* str,int i)
46 {
47 int *cur=&root;
48 int l=1;
49 for(const char*p=str;*p&&*cur;p++,l++)
50 {
51 cur=&child[*cur][*p-'a'];
52 if(flag[*cur])
53 {
54 dp[i]=(dp[i]+dp[i+l])%MOD;
55 }
56 }
57 return (*cur&&flag[*cur]);
58 }
59 }tree;
60
61 int main()
62 {
63 int cas=1;
64 while(scanf("%s",str)!=EOF)
65 {
66 int len=strlen(str);
67 scanf("%d",&m);
68 tree.Init();
69 while(m--)
70 {
71 char dic[120];
72 scanf("%s",dic);
73 tree.Insert(dic);
74 }
75 memset(dp,0,sizeof(dp));
76 dp[len]=1;
77 for(int i=len-1;i>=0;i--)
78 {
79 tree.query(str+i,i);
80 }
81 printf("Case %d: %d
",cas++,dp[0]%MOD);
82 }
83 return 0;
84 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.