HDU 6208 The Dominator of Strings【AC自动机/kmp/Sunday算法】
For each test case, the first line contains an integer N
indicating the size of the set.
Each of the following N
lines describes a string of the set in lowercase.
The total length of strings in each case has the limit of 100000
.
The limit is 30MB for the input file.
10
you
better
worse
richer
poorer
sickness
health
death
faithfulness
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
5
abc
cde
abcde
abcde
bcde
3
aaaaa
aaaab
aaaac
- #include<stdio.h>
- #include<string.h>
- #include<queue>
- #include<string>
- #include<iostream>
- #define maxlen 100005
- using namespace std;
- int n;
- int nxt[maxlen][],FAIL[maxlen],edd[maxlen],root,L;//nxt记录节点,在这里edd指针代表以当前节点为字符串尾的字符串个数
- int mark[maxlen];
- int newnode()
- {
- for(int i=;i<;i++)
- nxt[L][i]=-;//节点连接的边初始化为-1
- edd[L]=;
- mark[L]=;
- return L++;
- }
- void init()
- {
- L=;
- root=newnode();
- }
- void insert(char buf[],int l)//trie树的建立
- {
- int now=root;
- for(int i=;i<l;i++)
- {
- if(nxt[now][buf[i]-'a']==-)nxt[now][buf[i]-'a']=newnode();
- now=nxt[now][buf[i]-'a'];
- }
- edd[now]++;
- }
- void build()//建立ac自动机
- {
- queue<int>que;
- for(int i=;i<;i++)
- {
- if(nxt[root][i]==-)nxt[root][i]=root;
- else //若有连边则将节点加入队列 ,并将FAIL指针指向root
- {
- FAIL[nxt[root][i]]=root;
- que.push(nxt[root][i]);
- }
- }
- while(!que.empty())
- {
- int now=que.front();
- que.pop();
- for(int i=;i<;i++)
- {
- if(nxt[now][i]==-) //若无连边,则将该边指向当前节点FAIL指针指向的相应字符连接的节点
- nxt[now][i]=nxt[FAIL[now]][i];
- else //若有连边,则将儿子节点的FAIL指针指向当前节点FAIL指针指向相应字符接的节点
- {
- FAIL[nxt[now][i]]=nxt[FAIL[now]][i];
- que.push(nxt[now][i]); //加入队列继续遍历
- }
- }
- }
- }
- int query(char buf[],int l)
- {
- int now=root;
- int res=;
- for(int i=;i<l;i++)
- {
- now=nxt[now][buf[i]-'a'];
- int temp=now;
- while(temp!=root&&mark[temp]==)//根据题目要求改变形式
- {
- res+=edd[temp];
- edd[temp]=;
- mark[temp]=;
- temp=FAIL[temp];
- }
- }
- return res; //在这里返回的是匹配到的模式串的数量
- }
- char buf[maxlen],ans[maxlen];
- string A[maxlen];
- int main()
- {
- int T;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d",&n);
- init();
- int ma=;
- for(int i=;i<n;i++)
- {
- scanf("%s",buf);
- int l=strlen(buf);
- if(ma<l)
- {
- ma=l;
- strcpy(ans,buf);
- }
- insert(buf,l);
- }
- build();
- int sum=query(ans,ma);
- if(sum==n) puts(ans);
- else puts("No");
- }
- }
AC自动机
- #include <stdio.h>
- #include <string.h>
- char S[];
- char *t[],*s;
- int f[];
- void getfail(char p[],int f[]) //字符串p自我匹配
- {
- int len=strlen(p);
- f[]=f[]=;
- for(int i=;i<len;i++)
- {
- int j=f[i];
- while(j&&p[i]!=p[j])
- j=f[j];
- if(p[i]==p[j])
- f[i+]=j+;//多匹配到了一个字符
- else
- f[i+]=;//该字符配不上
- }
- }
- int find(char* T, char*P, int*f)//p去匹配字符串T
- {
- int n = strlen(T), m = strlen(P);
- getfail(P, f); //得出部分匹配表
- int j = ; //短串的下标
- for(int i = ; i < n; i++) //长串下标
- {
- while(j && P[j] != T[i])//突然失配了
- {
- j = f[j]; //j往回退,直到0或者上一个字符相等的位置
- }
- if(P[j] == T[i])
- {
- j++; //匹配了一个字符,j++
- }
- if(j == m) //短串匹配到头了
- {
- return ;//i - m + 1;//返回成功匹配的起点字符位置
- }
- }
- return -;
- }
- int main()
- {
- int T,n;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d",&n);
- int maxlen=;
- int p=;//记录最长串
- s=S;
- for(int i=;i<=n;i++)
- {
- scanf("%s",s);
- t[i]=s;
- if(strlen(s)>maxlen){
- maxlen=strlen(s);
- p=i;
- }
- s+=strlen(s)+;
- }
- int ans=;
- for(int i=;i<=n;i++)
- {
- if(find(t[p],t[i],f)==)
- ans++;
- else break;
- }
- if(ans==n)
- {
- printf("%s\n",t[p]);
- }
- else puts("No");
- }
- return ;
- }
KMP
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <iostream>
- #define ll long long
- using namespace std;
- int f[];
- int T,n;
- char s[];
- char *t[];
- void getfail(char p[]) //字符串p自我匹配
- {
- int len=strlen(p);
- f[]=f[]=;
- for(int i=;i<len;i++)
- {
- int j=f[i];
- while(j&&p[i]!=p[j])
- j=f[j];
- if(p[i]==p[j])
- f[i+]=j+;//多匹配到了一个字符
- else
- f[i+]=;//该字符配不上
- }
- }
- int find(char* T, char*P)//p去匹配字符串T
- {
- int n = strlen(T), m = strlen(P);
- getfail(P); //得出部分匹配表
- int j = ; //短串的下标
- for(int i = ; i < n; i++) //长串下标
- {
- while(j && P[j] != T[i])//突然失配了
- {
- j = f[j]; //j往回退,直到0或者上一个字符相等的位置
- }
- if(P[j] == T[i])
- {
- j++; //匹配了一个字符,j++
- }
- if(j == m) //短串匹配到头了
- {
- return ;//i - m + 1;//返回成功匹配的起点字符位置
- }
- }
- return -;
- }
- int main(){
- int max_len;
- scanf("%d",&T);
- while(T--){
- scanf("%d",&n);
- max_len=;
- int tmp;
- char *qw;
- char *io=s;
- for(int i= ;i <= n;i++){
- scanf("%s",io);
- tmp=strlen(io);
- if( tmp > max_len ){
- max_len=tmp;
- qw=io;
- }
- t[i]=io;
- io+=strlen(io)+;
- }
- int flag=;
- for(int j=;j<=n;j++){
- if( find(qw,t[j]) != ){
- flag=;
- break ;
- }
- }
- if(flag){
- printf("%s\n",qw);
- }
- else{
- printf("No\n");
- }
- }
- return ;
- }
another KMP
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<string>
- #include<vector>
- using namespace std;
- typedef long long int LL;
- int Sunday(string text, string pattern){
- int i = , j = , k;
- int m = pattern.size();
- if(pattern.size() <= || text.size() <= )
- return -;
- for(; i<text.size();) {
- if(text[i] != pattern[j]) {
- for(k=pattern.size() - ; k>=; k--) {
- if(pattern[k] == text[m])
- break;
- }
- i = m-k;
- j = ;
- m = i+pattern.size();
- }
- else {
- if(j == pattern.size()-)
- return i-j;
- i++;
- j++;
- }
- }
- return -;
- }
- vector<string> v;
- int main()
- {
- ios::sync_with_stdio(false);
- int T;
- cin>>T;
- while(T--)
- {
- int n;
- cin>>n;
- v.clear();
- string t,text;
- for(int i=;i<n;i++)
- {
- cin>>t;
- if(text.length()<t.length())
- text=t;
- v.push_back(t);
- }
- int f=;
- for(int i=;i<n;i++)
- {
- if(Sunday(text,v[i])== -)
- {
- f=;
- break;
- }
- }
- if(f)
- cout<<text<<endl;
- else
- cout<<"No"<<endl;
- }
- return ;
- }
Sunday algorithm
HDU 6208 The Dominator of Strings【AC自动机/kmp/Sunday算法】的更多相关文章
- HDU 6208 The Dominator of Strings 后缀自动机
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- hdu 6208 The Dominator of Strings【AC自动机】
hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...
- HDU 6208 The Dominator of Strings(AC自动机)
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- HDU - 6208 The Dominator of Strings HDU - 6208 AC自动机 || 后缀自动机
https://vjudge.net/problem/HDU-6208 首先可以知道最长那个串肯定是答案 然后,相当于用n - 1个模式串去匹配这个主串,看看有多少个能匹配. 普通kmp的话,每次都要 ...
- HDU 6208 The Dominator of Strings ——(青岛网络赛,AC自动机)
最长的才可能成为答案,那么除了最长的以外全部insert到自动机里,再拿最长的去match,如果match完以后cnt全被清空了,那么这个最长串就是答案.事实上方便起见这个最长串一起丢进去也无妨,而且 ...
- HDU 2222:Keywords Search(AC自动机模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意 ...
- SPOJ 7758. Growing Strings AC自动机DP
Growing Strings 题目:给出n个字符串,问最多能够选出多少个串组成序列,并满足前一个字符串是后一个字符串的子串. 分析: AC自动机经典水题... 考虑每个节点结尾时,他能够选出最多的串 ...
- hdu_5507_GT and strings(AC自动机)
题目链接:hdu_5507_GT and strings 题意:给n个字符串和q个询问,每个询问给两个数字x,y,问1.x是否为y的子序列,2.x是否为y的子串,是输出1,否则输出0,每个询问输出2个 ...
- HDU 2457/POJ 3691 DNA repair AC自动机+DP
DNA repair Problem Description Biologists finally invent techniques of repairing DNA that contains ...
随机推荐
- Codeforces数据结构(水题)小结
最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...
- BZOJ1407 [Noi2002]Savage 【扩展欧几里得】
题目链接 BZOJ1407 题解 枚举\(m\)用扩欧判即可 #include<algorithm> #include<iostream> #include<cstrin ...
- 【BZOJ 1930】 [Shoi2003]pacman 吃豆豆 最大费用最大流
如果你知道他是网络流的话你就很快会想到一个最大费用最大流的模型,然后你发现可能T,然而你发现你只用增广两次,然后你就开心的打了出来,然后发现被稠密图里spfa的丧病时间复杂度坑了,还是会T.于是我就开 ...
- 写一个JavaScript“返回顶部”功能
在web页面中,如果页面较高,为了方便用户快速地返回顶部,都会添加一个返回顶部按钮. 效果演示可以查看本页.如果页面有滚动高度,右下角就会有一个含有“返回顶部”字样的黑色背景半透明的小条条.点击这里“ ...
- 群联MPALL(Rel) 7F V5.03.0A-DL07量产工具 PS2251-07(PS2307)
前言:U盘被写保护,真的很醉人啊~~ 群联MPALL是一款群联PS2251系列主控量产修复工具,本版本支持PS2251-67.PS2251-68.PS2251-02.PS2251-03.PS ...
- video视频在结束之后回到初始状态
目前尝试了两种解决方案,但是方案1在安卓移动端无法生效(猜测是因为移动端安卓启动的是原生的视频播放控件的原因) 方案一: 重新load资源,这种方法比较简洁,但是在安卓下不适用 video.addEv ...
- 《vue.js实战》练习---标签页组件
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- commons
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --> <dependency> <g ...
- Java输入输出流备忘
重要博客: http://blog.csdn.net/hguisu/article/details/7418161 File dir = new File("\\root"); ...
- salt-api的使用
curl -k https://192.168.74.129:8006/ -H "Accept: application/x-yaml" -H "X-Auth-Token ...