HDU6208The Dominator of Strings 【AC自动机】
来源:互联网 发布:天谕萝莉捏脸数据小茗 编辑:程序博客网 时间:2024/06/04 17:56
传送门
显然只有长度最长的串才可能是答案
若有2个串长度最长 且不相同 无解
建立ac自动机,若存在>=2个深度最大的节点 无解
用最长的串跑一遍查询,如果能匹配到的串恰好有n个 答案就是该串
#include<stdio.h>#include<bits/stdc++.h>#define ll long long#define pii pair<int,int>#define pll pair<ll,ll>#define MEM(a,x) memset(a,x,sizeof(a))#define lowbit(x) ((x)&-(x))using namespace std;const int INF = 1e9+7;const int inf=INF;const int N = 100000 + 5;char str[N];bool vis[N];struct Trie{ const static int maxn=26e5+5; const static int BASE='a'; const static int N = 26; int next[maxn][N],end[maxn]; int father[maxn],dept[maxn]; char ch[maxn]; int root,L; //bool vis[N]; int newNode(){ MEM(next[L],-1); end[L++]=0; return L-1; } void init(){ L=0; root=newNode(); dept[root]=0; } void insert(char buf[]){ int now=root; for(int i=0;buf[i];++i){ int j=buf[i]-BASE; if(next[now][j]==-1){ int x=newNode(); dept[x]=dept[now]+1; ch[x]=buf[i]; father[x]=now; next[now][j]=x; } now=next[now][j]; } end[now]++; } //ac自动机 int fail[maxn]; void build(){ queue<int>que; fail[root]=root; for(int i=0;i<N;++i){ if(next[root][i]==-1){ next[root][i]=root; } else{ fail[next[root][i]]=root; que.push(next[root][i]); } } while(!que.empty()){ int now=que.front(); que.pop(); for(int i=0;i<N;++i){ if(next[now][i]==-1){ next[now][i]=next[fail[now]][i]; } else{ fail[next[now][i]]=next[fail[now]][i]; que.push(next[now][i]); } } } } bool check1(int&idx){//是否只有一个长度最长的串 idx=该串最后一个元素在自动机中下标 int maxDept=0; for(int i=0;i<L;++i){ maxDept=max(maxDept,dept[i]); } int deptNum=0; for(int i=0;i<L;++i){ if(dept[i]==maxDept){ idx=i; deptNum+=1; } } return deptNum==1; } ll getNum(int now){ ll ans=0; while(now!=root&&!vis[now]){ vis[now]=true; ans+=end[now]; end[now]=0; now=fail[now]; } return ans; } void rev(char*str){ int n=strlen(str); int i=0,j=n-1; while(i<j){ swap(str[i],str[j]); ++i; --j; } } void slove(int idx,int n){ fill(vis,vis+L+1,0); int now=idx; int sum=0; int ansIdx=0; while(now!=root){ sum+=getNum(now); str[ansIdx++]=ch[now]; now=father[now]; } if(sum==n){ str[ansIdx++]=0; rev(str); puts(str); } else{ puts("No"); } }}trie;int main(){ //freopen("/home/lu/code/r.txt","r",stdin); //freopen("/home/lu/code/w.txt","w",stdout); int T; scanf("%d",&T); while(T--){ int n; scanf("%d",&n); trie.init(); for(int i=0;i<n;++i){ scanf("%s",str); trie.insert(str); } int ed=0; if(!trie.check1(ed)){ puts("No"); continue; } trie.build(); trie.slove(ed,n); } return 0;}
阅读全文
0 0
- HDU6208The 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 AC自动机
- HDU 6208 The Dominator of Strings 读入挂+kmp / AC自动机
- HDU6208 The Dominator of Strings AC自动机 [2017 ACM/ICPC Asia Regional Qingdao Online]
- hdu2017青岛网络赛The Dominator of Strings(ac自动机练习题)
- 2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 HDU 6208 1003 The Dominator of Strings(AC自动机)
- HDU 6208 The Dominator of Strings 后缀自动机
- hdu 6208 The Dominator of Strings 后缀自动机 LCS
- The Dominator of Strings
- The Dominator of Strings
- 1003 The Dominator of Strings
- HDU6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings
- HDU6208 The Dominator of Strings【字符串】
- HDU
- 登录时设置了关联,但是一直登录未成功
- java --关键字
- poj 1745
- 传输数据库升级Oracle11.2.0.3到Oracle12.2
- HDU6208The Dominator of Strings 【AC自动机】
- 系统数据字典
- shell学习十四--编写监控mysql脚本
- 用户访问一个网站的过程描述
- Spark Streaming——DStream Transformation操作
- zuul 1.3.0 学习笔记
- Java--变量
- Java快捷键和命名规范
- 谈我的算法工程师校招经历