后缀自动机(不同子串的个数)hdu4416
来源:互联网 发布:java中有多继承吗 编辑:程序博客网 时间:2024/05/22 12:45
Online JudgeOnline ExerciseOnline TeachingOnline ContestsExercise AuthorF.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical ChartsBest Coder beta
VIP | STD Contests
Virtual Contests
DIY | Web-DIY beta
Recent Contests
Total Submission(s): 2503 Accepted Submission(s): 710
对A串建立后缀自动机,然后用其他的串去匹配,每个节点维护一个cnt,表示最大匹配长度,最后统计一下,不同的子串数量就可以了
Hand In Hand
Online Acmers
Forum | Discuss
Statistical ChartsBest Coder beta
VIP | STD Contests
Virtual Contests
DIY | Web-DIY beta
Recent Contests
【比赛提醒】BestCoder 你报名了吗?(点击报名)
【科普】什么是BestCoder?如何参加?
【科普】什么是BestCoder?如何参加?
Good Article Good sentence
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2503 Accepted Submission(s): 710
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?
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
32abababba1aaabbb2aaaaaaaaa
Sample Output
Case 1: 3Case 2: 3Case 3: 1
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;typedef long long LL;const int maxn=100010;const int SIGMA_SIZE=26;struct SAM_Node{ SAM_Node *par,*next[SIGMA_SIZE]; int len,id,pos; int cnt; SAM_Node() {} SAM_Node(int _len) { par=0; len=_len; cnt=0; memset(next,0,sizeof(next)); }};SAM_Node node[maxn*2],*root,*last;int SAM_size;SAM_Node *newSAM_Node(int len){ node[SAM_size]=SAM_Node(len); node[SAM_size].id=SAM_size; return &node[SAM_size++];}SAM_Node *newSAM_Node(SAM_Node *p){ node[SAM_size]=*p; node[SAM_size].id=SAM_size; return &node[SAM_size++];}void SAM_add(int x,int len){ SAM_Node *p=last,*np=newSAM_Node(p->len+1); np->pos=len; last=np; while(p&&!p->next[x]) { p->next[x]=np; p=p->par; } if(!p)np->par=root; else { SAM_Node *q=p->next[x]; if(q->len==p->len+1) np->par=q; else { SAM_Node *nq=newSAM_Node(q); nq->len=p->len+1; q->par=nq; np->par=nq; while(p&&p->next[x]==q) { p->next[x]=nq; p=p->par; } } }}void SAM_init(){ SAM_size=0; root=last=newSAM_Node(0); node[0].pos=0;}void SAM_build(char *s){ SAM_init(); int len=strlen(s); for(int i=0;i<len;i++) SAM_add(s[i]-'a',i+1);}char s[maxn];int N;int cnt[maxn*2];SAM_Node *sa[maxn*2];int main(){ int T,cas=1; scanf("%d",&T); while(T--) { scanf("%d",&N); scanf("%s",s); SAM_build(s); int len=strlen(s); for(int i=0;i<=len;i++)cnt[i]=0; for(int i=0;i<SAM_size;i++)cnt[node[i].len]++; for(int i=1;i<=len;i++)cnt[i]+=cnt[i-1]; for(int i=SAM_size-1;i>=0;i--)sa[--cnt[node[i].len]]=&node[i]; LL ans=0; while(N--) { scanf("%s",s); SAM_Node *p=root; int num=0; len=strlen(s); for(int i=0;i<len;i++) { int c=s[i]-'a'; if(p->next[c])num++,p=p->next[c]; else { while(p&&!p->next[c])p=p->par; if(p){num=p->len+1;p=p->next[c];} else{num=0,p=root;} } p->cnt=max(p->cnt,num); } } for(int i=SAM_size-1;i>=0;i--) { SAM_Node *p=sa[i]; if(p->cnt>0) { if(p->par)p->par->cnt=max(p->par->cnt,p->cnt); if(p->cnt<p->len)ans+=p->len-p->cnt; } else ans+=p->len-(p->par?p->par->len:0); } printf("Case %d: %I64d\n",cas++,ans); } return 0;}
0 0
- 后缀自动机(不同子串的个数)hdu4416
- HDU 4622 本质不同的子串个数:后缀自动机
- hdu4416[多串后缀自动机]
- HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)
- 后缀自动机(子串个数)spoj8222
- HDU 4622 Reincarnation(SAM 后缀自动机 求子串的不同子串个数)
- HDU4416(后缀自动机)
- hdu 4416 后缀自动机 求一个字符串中出现的不同子串的个数(去除一些其他字符串的子串)
- hdu4416——后缀自动机
- 后缀自动机 求不同的子串数目
- 后缀自动机 重复旋律 5(求所有本质不同的子串)
- ACdream 1430 SETI 后缀自动机/后缀数组 不重叠子串的个数
- 【后缀自动机】SPOJLCS SPOJNSUBSTR SPOJLCS2 HDU4416
- 【后缀自动机】SPOJLCS SPOJNSUBSTR SPOJLCS2 HDU4416
- spoj 694 Distinct Substrings(求不同的子串个数,后缀数组基础题)
- SPOJ 题目694 Distinct Substrings(后缀数组,求不同的子串个数)
- SPOJ 题目705 New Distinct Substrings(后缀数组,求不同的子串个数)
- 后缀自动机 重复旋律 7 求所有不同的子串的值的总和
- ui标签
- 1021. Deepest Root (25)
- Java初学者之接口与抽象类
- [Java]网络通信
- poj 2336 Ferry Loading II 贪心
- 后缀自动机(不同子串的个数)hdu4416
- 关于javascript 中的this 关键字的用法demo小代码
- Ubuntu Deepin安装JDK 配置环境变量
- 微信自动加好友 模拟位置(二)
- 缓存、缓存算法和缓存框架简介
- (转载)在mac系统安装Apache Tomcat的详细步骤
- jvm你知道哪些
- 一个使用javascript promise的例子
- 用Windows7+IDEA+SBT搭建Spark源代码调试与阅读环境