HDU 6138 Fleet of the Eternal Throne (2017多校8
来源:互联网 发布:java验证码过期怎么做 编辑:程序博客网 时间:2024/06/06 03:26
题意:
给你n 个串, q个查询, 每个查询 输出串x 和 串y 的最大公共子串 且这个串 是其他某些串的前缀。 输出长度。
思路:
因为场上读错题, 以为是个极水的后缀数组。= =
其实是ac自动机
我们给n 个串建立ac自动机。
字典树中每个节点维护一个 到根的距离(即是前缀的长度)
然后给串x 跑一遍自动机, 把经过的点的标记出来, 表示 这个点即是 x 的子串, 又是某个串的前缀。
然后在给y 跑一遍自动机, 当经过标记点时 更新一下答案即可, 因为这时候 这个串 即是x 的子串又是y 的子串 又是前缀。 更新答案好了。
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int maxn = 100000 + 10 << 1;struct Trie{ int len[maxn]; int next[maxn][26]; int fail[maxn]; int flag[maxn]; int root; int L; void init(){ L = 0; root = newnode(); len[root] = 0; } int newnode(){ for (int i = 0; i < 26; ++i){ next[L][i] = -1; } flag[L] = 0; return L++; } void insert(char* s){ int nod = root; int lens = strlen(s); for (int i = 0; i < lens; ++i){ int id = s[i] - 'a'; if (next[nod][id] == -1){ next[nod][id] = newnode(); } len[next[nod][id] ] = len[nod] + 1; nod = next[nod][id]; } } void build(){ queue<int>q; fail[root] = root; for (int i = 0; i < 26; ++i){ if (next[root][i] == -1){ next[root][i] = root; } else { fail[next[root][i] ] = root; q.push(next[root][i]); } } while(!q.empty()){ int u = q.front(); q.pop(); for (int i = 0; i < 26; ++i){ if (next[u][i] == -1){ next[u][i] = next[fail[u] ][i]; } else { fail[next[u][i] ] = next[fail[u] ][i]; q.push(next[u][i]); } } } } void update(char* s, int vis){ int lens = strlen(s); int nod = root; for (int i = 0; i < lens; ++i){ int id = s[i] - 'a'; nod = next[nod][id]; int tmp = nod; while(tmp != root){ flag[tmp] = vis; tmp = fail[tmp]; } } } int query(char* s, int vis){ int lens = strlen(s); int nod = root; int ans = 0; for (int i = 0; i < lens; ++i){ int id = s[i] - 'a'; nod = next[nod][id]; int tmp = nod; while(tmp != root){ if (flag[tmp] == vis){ ans = max(ans, len[tmp]); } tmp = fail[tmp]; } } return ans; }}ac;char s[maxn];int d;int pos[maxn];int main(){ int T; scanf("%d",&T); while(T--){ int n; scanf("%d", &n); d = 0; ac.init(); for (int i = 0; i < n; ++i){ pos[i] = d; scanf("%s", s + d); ac.insert(s + d); int len = strlen(s + d); d += len + 1; } ac.build(); int q; scanf("%d", &q); int vis = 1; while(q--){ int x, y; scanf("%d %d", &x, &y); --x;--y; ac.update(s + pos[x], vis); int ans = ac.query(s + pos[y], vis); ++vis; printf("%d\n", ans); } } return 0;}
Fleet of the Eternal Throne
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 173 Accepted Submission(s): 76
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
Recommend
liuyiding | We have carefully selected several similar problems for you: 6143 6142 6141 6140 6139
阅读全文
0 0
- 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
- hdu 6138 Fleet of the Eternal Throne
- HDU 6138 Fleet of the Eternal Throne
- 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(2017 Multi-University Training Contest 8)
- 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-2017 多校训练赛8-1006-Fleet of the Eternal Throne
- HDU 6138 Fleet of the Eternal Throne (后缀数组+字典树, 2017 Multi-Univ Training Contest 8)
- Period||POJ1961
- CF 590D Top Secret Task【dp递推+滚动数组】【好题】
- 2017多校8-1011killer name
- c#创建带参数的线程
- Java条形码生成技术-Barcode4j
- HDU 6138 Fleet of the Eternal Throne (2017多校8
- JavaScript中的call方法
- PAT 1036 跟奥巴马一起编程
- 2017第八次多校联合hdu6140(阅读题)
- Docker化tomcat并且使用maven热部署
- mfs分布式文件存储(终)
- C++通过构造器,实现对象初始化的两种方法(重载、默认参数)
- 逆地理编码及关键技术
- BZOJ 3160: 万径人踪灭(FFT+快速幂+manacher)