hdu 6138 Fleet of the Eternal Throne (ac自动机)
来源:互联网 发布:java调用url接口 编辑:程序博客网 时间:2024/06/07 19:03
Fleet of the Eternal Throne
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 521 Accepted Submission(s): 239
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 thex -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 exceed105 .
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
题意:
就是给你一张单词表,和m组询问,让你在每组询问a,b找第a个单词和第b个单词中最长的公共子串,且该子串要是单词表中一个单词的前缀(可以是a或b),输出长度
解析:
一道ac自动机的题,建好字典树后,只要在每组询问中,先将第a个串中每一个字符的的fail指针指向的所有节点全都标记一遍,(即将a串中子串是其他单词的前缀的单词全都标记一遍),再将b串也同样的方式遍历,如果再遍历的过程中遇到a串标记过的位置,记录该位置的长度即可(说明该单词的前缀也是a串的子串),随后取所有两次都遍历到的节点的长度的最大值就是答案了
还有需要注意的是这道题目字符串储存,因为题目没有给每一个串的最大长度,只给了所有串的总长度,看了大神的代码,是用一个新的数组index[i]来记录第i个字符的再str[]中的其实下标,这样就可以将所有的串都储存到一个str[1e5+10]的数组里了
#include<stdio.h>#include<string.h>#include<malloc.h>const int N =1e5+10;typedef struct node{node *next[26];node *fail;int sum;int mark;int len;//char c;}node;node *root;node* que[100010]; char str[N*10];int index[N];int ans;void init(node *a,int ll){for(int i=0;i<26;i++)a->next[i]=NULL;a->fail=NULL;a->sum=0;a->len=ll+1;a->mark=0;//a->c='\0';}void Insert(char word[]){node *p=root;for(int i=0;word[i];i++){int x=word[i]-'a';if(!p->next[x]){p->next[x]=(node *)malloc(sizeof(node));init(p->next[x],p->len);//p->c=x+'a';}p=p->next[x];}p->sum++;}void build_fail(){int head,tail;head=tail=0;que[tail++]=root;while(head<tail){node* tmp=que[head];head++;for(int i=0;i<26;i++){if(!tmp->next[i]) continue;if(tmp==root) tmp->next[i]->fail=root;else{node *p=tmp->fail;while(p){if(p->next[i]){tmp->next[i]->fail=p->next[i];break;}p=p->fail;}if(p==NULL) tmp->next[i]->fail=root;}que[tail++]=tmp->next[i];}}}void solve(char des[],char ff[]){int ans=0;node *p=root;for(int i=0;des[i]!='\0';i++){int x=des[i]-'a';p=p->next[x];node *temp;temp=p;while(temp!=root){temp->mark=1;temp=temp->fail;}}p=root;for(int i=0;ff[i]!='\0';i++) {int x=ff[i]-'a';p=p->next[x];node *temp;temp=p;while(temp!=root){if(temp->mark==1&&temp->len>ans){ans=temp->len;}temp=temp->fail;}}p=root;for(int i=0;des[i]!='\0';i++) //消除标记{int x=des[i]-'a';p=p->next[x];node *temp;temp=p;while(temp!=root){temp->mark=0;temp=temp->fail;}}printf("%d\n",ans);}int main(){int t,n,m,a,b;scanf("%d",&t);while(t--){root=(node*)malloc(sizeof(node));init(root,-1);scanf("%d",&n);getchar();int loca=0;for(int i=0;i<n;i++){index[i]=loca;scanf("%s",str+loca);Insert(str+loca);loca+=strlen(str+loca)+1;}build_fail();scanf("%d",&m);for(int i=0;i<m;i++){scanf("%d%d",&a,&b);solve(str+index[a-1],str+index[b-1]);}}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自动机&&思维】【静态数组】
- 剑指offer--二叉树的下一个节点
- Java BIO与NIO比较
- string 的一些用法——插入(insert)、删除(erase)、替换(replace)和查找(find)
- ubuntu16 安装pyinstaller 显示command not found
- 活动的四种启动模式(第一行代码总结)
- hdu 6138 Fleet of the Eternal Throne (ac自动机)
- MFC中遇到的关于error C4430: 缺少类型说明符
- 647. Palindromic Substrings
- 二叉排序树
- XML
- servlet生命周期
- CURL使用SSL证书访问HTTPS
- 最长递增子序列问题
- html ifram应用