HDU
来源:互联网 发布:淘宝超级店长有何用 编辑:程序博客网 时间:2024/05/28 23:09
题目:给一个字符串S和一系列字符串T1~Tn,问在S中有多少个不同子串满足它不是T1~Tn中任意一个字符串的子串。
思路:match[i]表示当前节点能匹配的最长长度
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<ctime>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3f//0x3f3f3f3fconst int MAXN = 200050, SIZE = 26;struct SAM { int len[MAXN], link[MAXN], next[MAXN][SIZE]; int total, last; inline int newNode(int L) { len[++total] = L; link[total] = 0; for(int i = 0; i < SIZE; ++i) next[total][i] = 0; return total; } inline void Add(int c) { int i, p = last, cur = newNode(len[last] + 1); for(; p && !next[p][c]; p = link[p]) next[p][c] = cur; if(!p) link[cur] = 1;//令其指向初始状态 else { int q = next[p][c]; if(len[q] == len[p] + 1) link[cur] = q; else {//> int clone = newNode(len[p] + 1); for(i = 0; i < SIZE; ++i) next[clone][i] = next[q][i]; link[clone] = link[q]; link[q] = link[cur] = clone;for(; p && next[p][c] == q; p = link[p]) next[p][c] = clone; } } last = cur; } void Init () {//根节点是1 total = 0; last = newNode(0); }}sam;char str[MAXN];int num[MAXN],idx[MAXN],match[MAXN];int main(){ int T,cas=0,n; scanf("%d",&T); while(T--){ scanf("%d",&n); scanf("%s",str); sam.Init(); for(int i=0;str[i]!='\0';i++) sam.Add(str[i]-'a'); mm(num,0); for(int i=1;i<=sam.total;i++) num[sam.len[i]]++; for(int i=1;i<=sam.total;i++) num[i]+=num[i-1]; for(int i=1;i<=sam.total;i++) idx[num[sam.len[i]]--]=i; mm(match,0); while(n--){ scanf("%s",str); int now=1,len=0; for(int i=0;str[i]!='\0';i++){ int c=str[i]-'a'; if(sam.next[now][c]!=0){ len++; now=sam.next[now][c]; } else{ while(sam.next[now][c]==0&&now!=0) now=sam.link[now]; if(now==0){ now=1; len=0; } else{ len=sam.len[now]+1; now=sam.next[now][c]; } } match[now]=max(match[now],len); } } for(int i=sam.total;i>=1;i--){ int p=idx[i],np=sam.link[p]; if(match[p]) match[np]=sam.len[np]; else match[p]=sam.len[np]; } LL sum=0; for(int i=1;i<=sam.total;i++) sum+=sam.len[i]-match[i]; printf("Case %d: %I64d\n",++cas,sum); } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 防止库函数无意间修改那些不受你控制的对象
- mybatis和ehcache缓存框架整合
- 百练2479:Maximum sum
- C-多线程-哲学家进餐问题
- 详解Hadoop Shuffle过程
- HDU
- 01-javaScript基本语法、数据类型、变量
- 2017.9.17开学第三周总结
- 电视剧集
- Go语言time package基础用法
- 周末日记--2017.9.17
- Leetcode练习 #5 Longest Palindromic Substring
- 订单生产线分配以及拆分逻辑实现
- 学习算法遇到的疑难杂症