HDU 6138 Fleet of the Eternal Throne AC自动机
来源:互联网 发布:web数据挖掘第二版pdf 编辑:程序博客网 时间:2024/06/16 15:23
题目链接:HDU 6138
Fleet of the Eternal Throne
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 862 Accepted Submission(s): 406
Problem Description
> The Eternal Fleet was built many centuries ago before the time of Valkorion by an unknown race on the planet of Iokath. The fate of the Fleet's builders is unknown but their legacy would live on. Its first known action was in the annihilation of all life in Wild Space. It spread across Wild Space and conquered almost every inhabited world within the region, including Zakuul. They were finally defeated by a mysterious vessel known as the Gravestone, a massive alien warship that countered the Eternal Fleet's might. Outfitted with specialized weapons designed to take out multiple targets at once, the Gravestone destroyed whole sections of the fleet with a single shot. The Eternal Fleet was finally defeated over Zakuul, where it was deactivated and hidden away. The Gravestone landed in the swamps of Zakuul, where the crew scuttled it and hid it away.
>
> — Wookieepedia
The major defeat of the Eternal Fleet is the connected defensive network. Though being effective in defensing a large fleet, it finally led to a chain-reaction and was destroyed by the Gravestone. Therefore, when the next generation of Eternal Fleet is built, you are asked to check the risk of the chain reaction.
The battleships of the Eternal Fleet are placed on a 2D plane ofn rows. Each row is an array of battleships. The type of a battleship is denoted by an English lowercase alphabet. In other words, each row can be treated as a string. Below lists a possible configuration of the Eternal Fleet.
aa
bbbaaa
abbaababa
abba
If in thex -th row and the y -th row, there exists a consecutive segment of battleships that looks identical in both rows (i.e., a common substring of the x -th row and y -th row), at the same time the substring is a prefix of any other row (can be the x -th or the y -th row), the Eternal Fleet will have a risk of causing chain reaction.
Given a query (x , y ), you should find the longest substring that have a risk of causing chain reaction.
>
> — Wookieepedia
The major defeat of the Eternal Fleet is the connected defensive network. Though being effective in defensing a large fleet, it finally led to a chain-reaction and was destroyed by the Gravestone. Therefore, when the next generation of Eternal Fleet is built, you are asked to check the risk of the chain reaction.
The battleships of the Eternal Fleet are placed on a 2D plane of
aa
bbbaaa
abbaababa
abba
If in the
Given a query (
Input
The first line of the input contains an integer T , denoting the number of test cases.
For each test cases, the first line contains integern (n≤105 ).
There aren lines following, each has a string consisting of lower case letters denoting the battleships in the row. The total length of the strings will not exceed 105 .
And an integerm (1≤m≤100 ) is following, representing the number of queries.
For each of the followingm lines, there are two integers x,y , denoting the query.
For each test cases, the first line contains integer
There are
And an integer
For each of the following
Output
You should output the answers for the queries, one integer per line.
Sample Input
13aaabaaacaaa22 31 2
Sample Output
33
Source
2017 Multi-University Training Contest - Team 8
题意:一堆字符串,总长度1e5,问两个串x,y,的最长公共子串是多少,并且这个子串是否个串的前缀。
题目分析:可以把所有的字符串写到一个数组里,然后AC自动机建树,对于每个节点记录其所在树的深度,然后对于没次查询,首先匹配x串,标记上经历过的节点,每个根到这个节点都是x的子串同时又是一个字符串的前缀,再匹配y即可,如果有标记过的节点就更新最大值。
//// main.cpp// HDU 6138 Fleet of the Eternal Throne//// Created by teddywang on 2017/9/21.// Copyright © 2017年 teddywang. All rights reserved.//#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;const int maxn=200010;const int maxm=200010;const int SIGMA_SIZE=26;int n,x,y,q,d,ans;int ls[maxn];char t[maxn],s[maxn];struct AC{ int ch[maxm][26]; int val[maxm]; int fail[maxm],last[maxm]; int vis[maxm]; int vs[maxm]; int sz; void clear() { memset(ch[0],0,sizeof(ch[0])); sz=1; memset(vis,0,sizeof(vis)); memset(vs,0,sizeof(vs)); } int idx(char x){return x-'a';} void insert(char *s) { int u=0; int n=strlen(s); for(int i=0;i<n;i++) { int c=idx(s[i]); if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; ch[u][c]=sz++; } vs[ch[u][c]]=vs[u]+1; u=ch[u][c]; } val[u]++; } void getfail() { queue<int> q; fail[0]=0; int u=0; for(int i=0;i<SIGMA_SIZE;i++) { u=ch[0][i]; if(u){q.push(u);fail[u]=0;last[u]=0;} } while(!q.empty()) { int r=q.front();q.pop(); for(int i=0;i<SIGMA_SIZE;i++) { u=ch[r][i]; if(!u){ch[r][i]=ch[fail[r]][i];continue;} q.push(u); int v=fail[r]; while(v&&!ch[v][i])v=fail[v]; fail[u]=ch[v][i]; last[u]=val[fail[u]]?fail[u]:last[fail[u]]; } } } void find(char *s,int pos) { int u=0; int n=strlen(s); for(int i=0;i<n;i++) { int c=idx(s[i]); u=ch[u][c]; int temp=0; temp=u; while(temp) { if(vis[temp]==pos) { ans=max(ans,vs[temp]); } temp=fail[temp]; } } } void update(char *s,int pos) { int u=0; int n=strlen(s); for(int i=0;i<n;i++) { int c=idx(s[i]); u=ch[u][c]; int temp=0; temp=u; while(temp) { vis[temp]=pos; temp=fail[temp]; } } }}tree;int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); int maxlen=0; d=0; tree.clear(); for(int i=1;i<=n;i++) { scanf("%s",t+d); tree.insert(t+d); int len=strlen(t+d); ls[i]=d; d+=len+1; } tree.getfail(); scanf("%d",&q); for(int i=1;i<=q;i++) { ans=0; scanf("%d%d",&x,&y); tree.update(t+ls[x],i); tree.find(t+ls[y],i); printf("%d\n",ans); } } return 0;}
阅读全文
0 0
- HDU 6138 Fleet of the Eternal Throne ( AC自动机)
- hdu 6138 Fleet of the Eternal Throne (ac自动机)
- HDU 6138 Fleet of the Eternal Throne [AC自动机]
- HDU 6138 Fleet of the Eternal Throne (AC自动机)
- hdu 6138 Fleet of the Eternal Throne(AC自动机)
- HDU 6138 Fleet of the Eternal Throne(AC自动机)
- HDU 6138 Fleet of the Eternal Throne AC自动机
- HDU 6138 2017多校第八场1006 Fleet of the Eternal Throne :AC自动机
- HDU 6138 Fleet of the Eternal Throne AC自动机||后缀数组
- HDU 6138 Fleet of the Eternal Throne 多校#8 AC自动机
- HDU 6138 Fleet of the Eternal Throne 多校#8 AC自动机
- HDU 6138 Fleet of the Eternal Throne
- hdu 6138 Fleet of the Eternal Throne
- HDU 6138 Fleet of the Eternal Throne
- hdu6138 Fleet of the Eternal Throne AC自动机
- HDU 6138 Fleet of the Eternal Throne (2017多校8
- HDU 6138 Fleet of the Eternal Throne (2017多校8
- HDU_6138 Fleet of the Eternal Throne 【AC自动机&&思维】【静态数组】
- ubuntu16.04 安装jre
- 8D模拟
- Android 图形系统之gralloc
- css动画停在最后状态
- springMVC 执行流程
- HDU 6138 Fleet of the Eternal Throne AC自动机
- Idea导入项目及部署至Tomcat(从Eclipse到Idea的华丽转身)
- 关于jedis异常:Could not get a resource from the pool
- Thinkphp5 出现错误 require mongodb > 1.0
- DrawerLayout侧滑栏
- OPENGL学习笔记之三
- 关于V4L2编程中获取视频流的误区
- hdu4460-最短路&思维- Friend Chains
- 案例|FEC筷云经典案例“西洋街”,澳门最大跨境电商平台