hdu 5069 Harry And Biological Teacher

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

Problem Description

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?

Input

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.

Output

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.

Sample Input

2 1

ACCGT

TTT

1 2

4 4

AAATTT

TTTCCC

CCCGGG

GGGAAA

1 2

2 3

3 4

4 1

Sample Output

1

3

3

3

3

Source

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
*/

时间: 2024-12-13 11:43:17

hdu 5069 Harry And Biological Teacher的相关文章

HDU 5069 Harry And Biological Teacher(AC自动机+线段树)

题意 给定 \(n\) 个字符串,\(m\) 个询问,每次询问 \(a\) 字符串的后缀和 \(b\) 字符串的前缀最多能匹配多长. \(1\leq n,m \leq 10^5\) 思路 多串匹配,考虑 \(\text{AC}\)自动机,对 \(n\) 个串建自动机,观察这个结构,不难发现 \(Trie\) 树的结构和前缀有关,\(fail\) 树的结构和后缀有关. 考虑离线,对于每个 \(b\) ,存储它对应的 \(a\) ,我们通过在自动机上扫 \(b\) 来回答.由于扫到某节点 \(u\)

HDU 5056 Harry And Biological Teacher

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5069 题意:给出n个串,m个询问,每个询问(u,v),求u的一个最长后缀是v的前缀. 思路:离线.将关于u的后缀的查询放在一起,然后将u插入后缀自动机.对于每个v跑一遍即可. struct SAM { SAM *son[4],*pre; int len; int ok; void init() { clr(son,0); pre=0; ok=0; } }; SAM sam[N],*head,*last;

hdu 5894 hannnnah_j’s Biological Test 组合数学

传送门:hdu 5894 hannnnah_j’s Biological Test 题目大意:n个座位,m个学生,使每个学生的间隔至少为k个座位 组合中的插空法 思路:每个学生先去掉k个空位间隔,剩下n-k*m:这些空位至少要坐m个学生,n-k*m-1个空,插m-1个门,方法数为:c(n-k*m-1,m-1):当只有一个学生时,间隔K个位的条件就没必要了,也就是n>k+1的条件不一定要成立 顺带弄了个Lucas的模板 /**************************************

HDU 5066 Harry And Physical Teacher(物理题)

HDU 5066 Harry And Physical Teacher 思路:利用物理里面的动量守恒公式,由于保证小车质量远大于小球,所以可以把小车质量当成无穷大带进去,得到答案为2 * v0 - v; 代码: #include <cstdio> #include <cstring> #include <vector> using namespace std; typedef long long ll; int v, v0; int main() { while (~s

HDU 5068 Harry And Math Teacher( 矩阵乘法 + 线段树维护 )

HDU 5068 Harry And Math Teacher( 矩阵乘法 + 线段树维护 ) 题意: 首先是这题题意理解错误,,其次是这题无法理解状态... 已经不是英文有多烂的情况了,是中文没学好啊.....大学不学语文才是真正的硬伤啊 题目意思 有一个城堡有很多层楼, 每层楼有2个门,每个门里面又有两个楼梯,可以通往上一层的两个门 问,从x层楼到y层楼有多少中方法(不能返回) 具体看图吧,,,已经不会说话了 1 #include <cstdio> 2 #include <cstri

HDU - 5066 Harry And Physical Teacher ( 物理-动量守恒+能量守恒 )

HDU - 5066 Harry And Physical Teacher 昨天BC的第一题,当时WA一发,一开始直接觉得是碰撞后直接反弹. 直接上官方题解 我觉得还是应该叫做能量守恒 和 动量守恒 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <set> #include <stack> #include

Harry And Biological Teacher 分块 + hash

http://acm.hdu.edu.cn/showproblem.php?pid=5069 首先判断suffix和prefix最长多少可以直接暴力枚举长度然后 + hash可以立马判断是否相等,复杂度O(lenstr) 现在知道总长度 <= 1e5,  magic = sqrt(lenstr) 那么, 如果长度是 <= magic的,最多1e5个询问,每次询问我暴力也是才1e5 * sqrt(1e5)的复杂度. 如果长度是 >= magic的, 第一个极端情况是有magic个不同的串,

hdu 5066 Harry And Physical Teacher(Bestcoder Round #14)

Harry And Physical Teacher Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 91    Accepted Submission(s): 68 Problem Description As we all know, Harry Porter learns magic at Hogwarts School. How

HDU 5068 Harry And Math Teacher

主题链接~~> 做题情绪:的非常高深,有种高大上的感觉. 解题思路: 两层之间的联通能够看成是一个矩阵  代表上下两层都能够联通,,代表下层第1个门与上层第一个门不联通,以此类推联通就能够用矩阵表示了.这样改动和查询都能够用线段树来完毕. 代码: #include<iostream> #include<sstream> #include<map> #include<cmath> #include<fstream> #include<q