HDU 4416 Good Article Good sentence
Good Article Good sentence
Time Limit: 3000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 441664-bit integer IO format: %I64d Java class name: Main
In middle school, teachers used to encourage us to pick up pretty sentences so that we could apply those sentences in our own articles. One of my classmates ZengXiao Xian, wanted to get sentences which are different from that of others, because he thought the distinct pretty sentences might benefit him a lot to get a high score in his article.
Assume that all of the sentences came from some articles. ZengXiao Xian intended to pick from Article A. The number of his classmates is n. The i-th classmate picked from Article Bi. Now ZengXiao Xian wants to know how many different sentences she could pick from Article A which don't belong to either of her classmates?Article. To simplify the problem, ZengXiao Xian wants to know how many different strings, which is the substring of string A, but is not substring of either of string Bi. Of course, you will help him, won't you?
Assume that all of the sentences came from some articles. ZengXiao Xian intended to pick from Article A. The number of his classmates is n. The i-th classmate picked from Article Bi. Now ZengXiao Xian wants to know how many different sentences she could pick from Article A which don't belong to either of her classmates?Article. To simplify the problem, ZengXiao Xian wants to know how many different strings, which is the substring of string A, but is not substring of either of string Bi. Of course, you will help him, won't you?
Input
The first line contains an integer T, the number of test data.
For each test data
The first line contains an integer meaning the number of classmates.
The second line is the string A;The next n lines,the ith line input string Bi.
The length of the string A does not exceed 100,000 characters , The sum of total length of all strings Bi does not exceed 100,000, and assume all string consist only lowercase characters 'a' to 'z'.
For each test data
The first line contains an integer meaning the number of classmates.
The second line is the string A;The next n lines,the ith line input string Bi.
The length of the string A does not exceed 100,000 characters , The sum of total length of all strings Bi does not exceed 100,000, and assume all string consist only lowercase characters 'a' to 'z'.
Output
For each case, print the case number and the number of substrings that ZengXiao Xian can find.
Sample Input
3 2 abab ab ba 1 aaa bbb 2 aaaa aa aaa
Sample Output
Case 1: 3 Case 2: 3 Case 3: 1
Source
题意:给一个字符串S和一系列字符串T1~Tn,问在S中有多少个不同子串满足它不是T1~Tn中任意一个字符串的子串
解题:后缀自动机
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 100010; 5 int sa[maxn<<1],c[maxn]; 6 struct node{ 7 int son[26],f,len,deep; 8 void init(){ 9 memset(son,-1,sizeof son); 10 deep = len = 0; 11 f = -1; 12 } 13 }; 14 struct SAM{ 15 node e[maxn<<1]; 16 int tot,last; 17 int newnode(int len = 0){ 18 e[tot].init(); 19 e[tot].len = len; 20 return tot++; 21 } 22 void init(){ 23 tot = last = 0; 24 newnode(); 25 } 26 void add(int c){ 27 int p = last,np = newnode(e[p].len + 1); 28 while(p != -1 && e[p].son[c] == -1){ 29 e[p].son[c] = np; 30 p = e[p].f; 31 } 32 if(p == -1) e[np].f = 0; 33 else{ 34 int q = e[p].son[c]; 35 if(e[p].len + 1 == e[q].len) e[np].f = q; 36 else{ 37 int nq = newnode(); 38 e[nq] = e[q]; 39 e[nq].len = e[p].len + 1; 40 e[q].f = e[np].f = nq; 41 while(p != -1 && e[p].son[c] == q){ 42 e[p].son[c] = nq; 43 p = e[p].f; 44 } 45 } 46 } 47 last = np; 48 } 49 void update(char *str){ 50 int ln = 0,p = 0; 51 for(int i = 0; str[i]; ++i){ 52 int c = str[i] - 'a'; 53 if(e[p].son[c] != -1){ 54 p = e[p].son[c]; 55 e[p].deep = max(e[p].deep,++ln); 56 }else{ 57 while(p != -1 && e[p].son[c] == -1) p = e[p].f; 58 if(p == -1) p = ln = 0; 59 else{ 60 ln = e[p].len + 1; 61 p = e[p].son[c]; 62 e[p].deep = max(e[p].deep,ln); 63 } 64 } 65 } 66 } 67 LL count(){ 68 LL ret = 0; 69 for(int i = tot-1; i > 0; --i){ 70 int v = sa[i]; 71 if(e[v].deep > 0){ 72 e[e[v].f].deep = max(e[e[v].f].deep,e[v].deep); 73 if(e[v].deep < e[v].len) ret += e[v].len - e[v].deep; 74 }else ret += e[v].len - e[e[v].f].len; 75 } 76 return ret; 77 } 78 }sam; 79 char str[maxn]; 80 int main(){ 81 int kase,m,cs = 1; 82 scanf("%d",&kase); 83 while(kase--){ 84 scanf("%d%s",&m,str); 85 sam.init(); 86 for(int i = 0; str[i]; ++i) sam.add(str[i] - 'a'); 87 memset(c,0,sizeof c); 88 for(int i = 0; i < sam.tot; ++i) c[sam.e[i].len]++; 89 for(int i = 1,len = strlen(str); i <= len; ++i) c[i] += c[i-1]; 90 for(int i = sam.tot-1; i >= 0; --i) sa[--c[sam.e[i].len]] = i; 91 while(m--){ 92 scanf("%s",str); 93 sam.update(str); 94 } 95 printf("Case %d: %I64d\n",cs++,sam.count()); 96 } 97 return 0; 98 }
夜空中最亮的星,照亮我前行