HDU 6208 The Dominator of Strings AC自动机
来源:互联网 发布:淘宝闲鱼网官网 编辑:程序博客网 时间:2024/06/12 19:11
题意:长度最长串为母串 问其余n-1个串是否都能在母串中匹配 ,总字符长度<=1e5.
AC自动机匹配数为n则有解.
query时不重复统计以标记结点.
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N=1e5+20,M=1e5+5; struct AHO{int chd[N][26],v[N],f[N],last[N],sz,ans;void init(){sz=1;ans=0;memset(v,0,sizeof(v));memset(f,0,sizeof(f));memset(chd[0],0,sizeof(chd[0]));}void insert(char *s){int u=0,i=0;while(s[i]){int id=s[i]-'a';if(!chd[u][id]){memset(chd[sz],0,sizeof(chd[sz]));chd[u][id]=sz++;}u=chd[u][id];i++; }v[u]++;}void getfails(){queue<int> q;f[0]=0;for(int c=0;c<26;c++){int u=chd[0][c];if(u){f[u]=0;q.push(u);last[u]=0;}}while(!q.empty()){int r=q.front();q.pop();for(int c=0;c<26;c++){int u=chd[r][c];if(!u){chd[r][c]=chd[f[r]][c];}else{q.push(u);int x=f[r];while(x&&!chd[x][c]) x=f[x];f[u]=chd[x][c];last[u]=v[f[u]]? f[u]:last[f[u]];}}}} int find(char *s) { int u=0,cnt=0; int n=strlen(s); for(int i=0;i<n;i++) { int c=s[i]-'a'; u=chd[u][c]; int temp=0;//mark if(v[u]) temp=u; else if(last[u]) temp=last[u]; while(temp) { cnt+=v[temp]; v[temp]=0; temp=last[temp]; } } return cnt; }}aho;char s[M],dic[M];int main(){int t;scanf("%d",&t);while(t--){aho.init();int n,mx=0;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%s",dic);int tl=strlen(dic);if(mx<tl){mx=tl;strcpy(s,dic);}aho.insert(dic);}aho.getfails();//int ans=aho.find(s);//printf("%d\n",ans);if(ans==n)printf("%s\n",s);elseprintf("No\n");}return 0;}
阅读全文
0 0
- HDU 6208 The Dominator of Strings [AC自动机]
- HDU 6208 The Dominator of Strings AC自动机
- HDU 6208 The Dominator of Strings AC自动机
- HDU 6208 The Dominator of Strings (AC自动机)
- HDU 6208 The Dominator of Strings AC自动机
- HDU 6208 The Dominator of Strings 读入挂+kmp / AC自动机
- HDU 6208 The Dominator of Strings 后缀自动机
- hdu 6208 The Dominator of Strings 后缀自动机 LCS
- 2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 HDU 6208 1003 The Dominator of Strings(AC自动机)
- HDU6208The Dominator of Strings 【AC自动机】
- HDU 6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings
- HDU6208 The Dominator of Strings AC自动机 [2017 ACM/ICPC Asia Regional Qingdao Online]
- hdu2017青岛网络赛The Dominator of Strings(ac自动机练习题)
- 同样为了翻译
- ListView多条目
- spring源码-ApplicationContext
- Python多进程共享变量
- 面向对象(⑦-⑨)
- HDU 6208 The Dominator of Strings AC自动机
- android 完全退出应用程序
- 无限轮播异步加载
- UVA
- 算法
- 集合-集合大家族
- XLisetView+ViewPager无限轮播+多条目加载
- QQ返回头像
- UVA