您的位置:首页 > 其它

hdu 5069 Harry And Biological Teacher

2014-10-19 00:42 459 查看

Harry And Biological Teacher

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 38 Accepted Submission(s): 6


[align=left]Problem Description[/align]
As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm.
Today, Harry is on his biological class, his teacher is doing experiment with DNA right now. But the clever teacher faces a difficult problem. He has lots of genes. Every time he picks gene a and gene b. If he want to connect gene a and gene b, he should calculate the length of longest part that the gene a’s suffix and gene b’s prefix can overlap together. For example gene a is "AAT" and gene b is "ATT", then the longest common part is "AT", so the answer is 2. And can you solve this difficult problem for him?

[align=left]Input[/align]
They are sever test cases, you should process to the end of file.
For each test case, there are two integers n and m (1≤n≤100000,1≤m≤100000)on the first line, indicate the number of genes and the number of queries.
The next following n line, each line contains a non-empty string composed by characters 'A' , 'T' , 'C' , 'G'. The sum of all the characters is less than 100000.
Then the next following m line, each line contains two integers a and b(1≤a,b≤n), indicate the query.

[align=left]Output[/align]
For each query, you should output one line that contains an integer indicate the length of longest part that the gene a’s suffix and gene b’s prefix can overlap together.

[align=left]Sample Input[/align]

2 1

ACCGT

TTT

1 2

4 4

AAATTT

TTTCCC
CCCGGG

GGGAAA

1 2

2 3

3 4

4 1

[align=left]Sample Output[/align]

1

3

3

3

3

[align=left]Source[/align]
BestCoder Round #14
思路:离线处理询问,记录 a下面的所有b询问,然后每次都对 a建立后缀自动机

然后记录 a的末尾节点,然后用 a下的b 去匹配,遇到末尾节点就判断。失配就退出

这里加个优化,对于a下的询问,经行排序,这样就可以去掉重复的询问

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define mod 1000000007
#define maxn 200010
#define INF 0x3f3f3f3f
using namespace std;

struct node
{
int u,id;
bool operator<(const node&s)const
{
return u < s.u ;
}
};
struct SAM
{
SAM *pre,*son[4] ;
int len ,vi;
void init()
{
pre=NULL ;
memset(son,NULL,sizeof(son)) ;
vi=0;
}
}que[maxn*2],*root,*tail,*b[maxn];
int tot,ans[maxn] ,cnt[maxn],s[maxn];
int id(char a )
{
if(a=='A') return 0 ;
else if(a=='C') return 1 ;
else if(a=='G') return 2 ;
return 3 ;
}
vector<node>qe[maxn] ;
void add(int c ,int l)
{
que[tot].init();
SAM *p = tail,*np=&que[tot++] ;
np->len=l;tail=np ;
while(p&&p->son[c]==NULL)p->son[c]=np,p=p->pre ;
if(p==NULL) np->pre = root ;
else
{
SAM *q = p->son[c] ;
if(p->len+1==q->len)np->pre = q ;
else
{
que[tot].init();
SAM *nq = &que[tot++] ;
*nq=*q ;
nq->len = p->len+1;
np->pre=q->pre=nq;
while(p&&p->son[c]==q) p->son[c]=nq,p=p->pre;
}
}
}
char str[maxn] ;
int main()
{
int i , j ,k ,len,n , m;
int u,v ;
node a ;
while(scanf("%d%d",&n,&m) != EOF)
{
for(i=1;i<=n;i++)
qe[i].clear();
len=1;
for( i = 1 ; i <= n ;i++)
{
scanf("%s",str+len) ;
k = strlen(str+len) ;
s[i]=len;
cnt[i]=k;
len += k+1 ;
}
for( i = 1 ; i <= m ;i++)
{
scanf("%d%d",&u,&v) ;
a.id = i ;
a.u = v ;
qe[u].push_back(a);
}
for( i = 1 ; i <= n ;i++)
{
if(qe[i].size()==0) continue ;
sort(qe[i].begin(),qe[i].end());
tot=0;
que[0].init();
root=tail=&que[tot++];
k=1;
for( j = s[i] ; j < s[i]+cnt[i];j++){
add(id(str[j]),k++);
}
SAM *p= tail;
while(p != NULL )
{
p->vi = true;
p=p->pre;
}

for( j = 0 ; j < qe[i].size();j++){
a=qe[i][j] ;
ans[a.id]=0;
if(j&&a.u==qe[i][j-1].u)
{
ans[a.id]=ans[qe[i][j-1].id];
continue ;
}
p = root ;
int tmp=0;
for( k = s[a.u] ; k < s[a.u]+cnt[a.u];k++)
{
int v = id(str[k]) ;
if(p->son[v]==NULL) break ;
p=p->son[v] ;
tmp++;
if(p->vi)
ans[a.id]=max(ans[a.id],tmp);
}
}
}
for( i = 1 ; i <= m ;i++)
printf("%d\n",ans[i]);
}
return 0 ;
}
/*
4 4
AAATTT
TTTCCC
CCCGGG
GGGAAA
2 3
1 2
3 4
4 1
*/


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: