HDU 4416 Good Article Good sentence 后缀自动机
来源:互联网 发布:一战往事知乎 编辑:程序博客网 时间:2024/04/27 22:16
求字符串A与B,求不为B子串的A子串的个数。
与SPOJ 1812反过来。。
记得数据要沿Parent树更新。。
#include <cstring>#include <cstdio>#include <algorithm>using namespace std;#define FOR(i,j,k) for(i=j;i<=k;++i)const int rt = 1, N = 200005;int last = 1, cnt = 1, len = 0;int ch[N][26], fa[N], ma[N], rr[N], b[N], bucket[N >> 1];char str[N >> 1];void add(char c) { int np = ++cnt, p = last, q, nq; last = np; ma[np] = ++len; rr[np] = 0; memset(ch[np], 0, sizeof ch[np]); while (p && !ch[p][c]) ch[p][c] = np, p = fa[p]; if (!p) fa[np] = rt; else { q = ch[p][c]; if (ma[q] == ma[p] + 1) fa[np] = q; else { nq = ++cnt; memcpy(ch[nq], ch[q], sizeof ch[q]); ma[nq] = ma[p] + 1; rr[nq] = 0; fa[nq] = fa[q]; fa[q] = fa[np] = nq; while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p]; } }}int main() { int i, n, t, p, y, c, Q, kase = 0; long long ans; scanf("%d", &Q); while (Q--) { scanf("%d", &t); last = rt; cnt = 1; ans = len = 0; memset(ch[rt], 0, sizeof ch[rt]); scanf("%s", str); n = strlen(str); for (i = 0; str[i]; ++i) add(str[i] - 'a'); FOR(i,0,n) bucket[i] = 0; FOR(i,1,cnt) ++bucket[ma[i]]; FOR(i,1,n) bucket[i] += bucket[i - 1]; for (i = cnt; i; --i) b[bucket[ma[i]]--] = i; while (t--) { scanf("%s", str); p = rt; y = 0; for (i = 0; str[i]; ++i) { c = str[i] - 'a'; if (ch[p][c]) { p = ch[p][c]; rr[p] = max(rr[p], ++y); } else { while (p && !ch[p][c]) p = fa[p]; if (p) y = ma[p] + 1, p = ch[p][c], rr[p] = max(rr[p], y); else p = rt, y = 0; } } } for(i=cnt;i;--i) if (rr[b[i]]) { rr[fa[b[i]]] = max(rr[fa[b[i]]], rr[b[i]]); if (rr[b[i]] < ma[b[i]]) ans += ma[b[i]] - rr[b[i]]; } else ans += ma[b[i]] - ma[fa[b[i]]]; printf("Case %d: %lld\n", ++kase, ans); } return 0;}
Good Article Good sentence
Problem Description
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?
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’.
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
2012 ACM/ICPC Asia Regional Hangzhou Online
Recommend
liuyiding
- hdu 4416 Good Article Good sentence (后缀自动机 SAM)
- HDU 4416 Good Article Good sentence 后缀自动机
- Hdu 4416 Good Article Good sentence 后缀自动机
- [HDOJ 4416] Good Article Good sentence [后缀自动机]
- hdu 4416 Good Article Good sentence ( 后缀数组 )
- hdu 4416 Good Article Good sentence (后缀数组)
- [后缀数组+思路] hdu 4416 Good Article Good sentence
- hdu 4416 Good Article Good sentence(后缀数组&思维)
- hdu 4416 Good Article Good sentence (后缀数组)
- HDU 4416 Good Article Good sentence(后缀数组)
- 【HDU 4416 】Good Article Good sentence 后缀数组
- HDU 4416 Good Article Good sentence
- hdu 4416 Good Article Good sentence
- HDU 4416 Good Article Good sentence(SAM)
- HDU 4416 Good Article Good sentence
- HDU 4416Good Article Good sentence
- Hdu 4416 Good Article Good sentence
- Good Article Good sentence Hdu
- 福州大学第十二届程序设计竞赛-H - 最小花费
- HashSet下的字符串排序,关键字,HashSet转TreeSet()
- flask安装mysql-python出错(环境ubuntu14.04)
- 高级工程师面试经验宝典
- XXE漏洞——XML实体定义引起的兼容性问题
- HDU 4416 Good Article Good sentence 后缀自动机
- JAVA面试经验宝典
- web.xml文件的作用及基本配置
- iOS数据库离线缓存思路和网络层封装——数据缓存操作封装
- LinkedList搭配collections.sort做字符串排序。针对上文精简了方法的实现部分
- 关于Spring mvc 一次请求Controller执行两次的问题
- S3C2451_nand flash_ARM操作读写源码
- Leetcode题解 155. Min Stack
- ssh: connect to host localhost port 22: Connection refused 问题