题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138
题意:给出n个串以及m个询问,对于每个询问(x, y),回答串x和串y的最长公共连续子串,并且这个串还是n个串中任意一个串的前缀。
思路:将这n个串建立AC自动机,每个节点保存其到根节点的距离(即前缀的长度)。首先将串x在自动机上跑一遍,将经过的节点做个标记(表示串x可以匹配到这些节点所代表的前缀),再把串y在自动机上跑一遍,若其经过的节点已经存在标记,则说明这个节点所代表的前缀既能匹配x也能匹配y,更新最大答案即可。
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<iostream>
#include<set>
#include<map>
#include<cmath>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#define fin freopen("1006.in","r",stdin)
#define fout freopen("a.txt","w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int, int> P;
const int INF = 1e8 + 10;
const int maxn = 1e5 + 10;
const int maxnode = 1e5 + 10;
const int sigma_size = 26;
string text[maxn];
char in[maxn];
int vis, ans;
struct AC
{
int ch[maxnode][sigma_size];
int val[maxnode];
int f[maxnode];
int last[maxnode];
int flag[maxnode];
int sz;
void init() { sz = 1; memset(ch[0], 0, sizeof ch[0]); }
int idx(char c) { return c - 'a'; }
void insert(string &s)
{
int u = 0, n = s.size();
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] = i+1;
ch[u][c] = sz++;
}
u = ch[u][c];
}
}
int getFail()
{
queue<int> q;
f[0] = 0;
for(int c = 0; c < sigma_size; c++)
{
int u = ch[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 < sigma_size; c++)
{
int u = ch[r][c];
if(!u) { ch[r][c] = ch[f[r]][c]; continue; }
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
}
void print1(int j)
{
if(!j) return;
if(flag[j] != vis) flag[j] = vis;
print1(last[j]);
}
void print2(int j)
{
if(!j) return;
if(flag[j] == vis) ans = max(ans, val[j]);
print2(last[j]);
}
void Find1(string &T)
{
int n = T.length();
int j = 0;
for(int i = 0; i < n; i++)
{
int c = idx(T[i]);
j = ch[j][c];
if(val[j]) print1(j);
else if(last[j]) print1(last[j]);
}
}
void Find2(string &T)
{
int n = T.length();
int j = 0;
for(int i = 0; i < n; i++)
{
int c = idx(T[i]);
j = ch[j][c];
if(val[j]) print2(j);
else if(last[j]) print2(last[j]);
}
}
}ac;
int main()
{
//fin; fout;
int T;
scanf("%d", &T);
while(T--)
{
ac.init();
int n, m, x, y;
scanf("%d", &n);
for(int i = 1; i <= n; i++) { scanf("%s", in); text[i] = in; ac.insert(text[i]);}
ac.getFail();
scanf("%d", &m);
while(m--)
{
ans = 0;
++vis;
scanf("%d%d", &x, &y);
ac.Find1(text[x]); ac.Find2(text[y]);
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): 568 Accepted Submission(s): 264
>
> — 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 n 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 the x -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.
For each test cases, the first line contains integer n ( n≤105 ).
There are n 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 integer m ( 1≤m≤100 ) is following, representing the number of queries.
For each of the following m lines, there are two integers x,y , denoting the query.
1 3 aaa baaa caaa 2 2 3 1 2
3 3
4409



被折叠的 条评论
为什么被折叠?



