链接: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;
int cnt;

void initSam()
{
    head=last=&sam[0];
	head->init();
    cnt=1;
}

int get(char x)
{
	if(x=='A') return 0;
	if(x=='T') return 1;
	if(x=='G') return 2;
	return 3;
}

void insert(int x)
{
    SAM *p=&sam[cnt++],*u=last;

	p->init();

    p->len=last->len+1;
    last=p;
    for(;u&&!u->son[x];u=u->pre) u->son[x]=p;
    if(!u) p->pre=head;
    else if(u->son[x]->len==u->len+1) p->pre=u->son[x];
    else
    {
        SAM *r=&sam[cnt++],*q=u->son[x];
        *r=*q; r->len=u->len+1;
        p->pre=q->pre=r;
        for(;u&&u->son[x]==q;u=u->pre) u->son[x]=r;
    }
}

char s[N*2];
int cur;

int start[N],len[N];

vector<pair<int,int> > V[N];

int ans[N];

map<int,int> mp;

int n,m;

int main()
{
//	FFF;

	while(scanf("%d%d",&n,&m)!=-1)
	{
		cur=0;
		int i;
		for(i=1;i<=n;i++)
		{
			scanf("%s",s+cur);
			start[i]=cur;
			len[i]=strlen(s+cur);
			cur+=len[i]+3;
		}
		for(i=1;i<=m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			V[u].pb(MP(v,i));
		}

		for(i=1;i<=n;i++) if(SZ(V[i])>0)
		{
			int j;
			initSam();
			for(j=start[i];s[j];j++)  insert(get(s[j]));
			SAM *p=last;
			while(p!=NULL) p->ok=1,p=p->pre;

			mp.clear();
			for(j=0;j<SZ(V[i]);j++)
			{
				int k=V[i][j].first;
				int id=V[i][j].second;
				if(mp.count(k))
				{
					ans[id]=mp[k];
					continue;
				}
				SAM *p=head;
				int t,tmp=0,cur=0;
				for(t=start[k];s[t];t++)
				{
					int x=get(s[t]);
					if(p->son[x])
					{
						cur++;
						p=p->son[x];
						if(p->ok) tmp=max(tmp,cur);
					}
					else break;
				}
				mp[k]=tmp;
				ans[id]=tmp;
			}
			V[i].clear();
		}
		for(i=1;i<=m;i++) printf("%d\n",ans[i]);
	}
}

HDU 5059 Harry And Biological Teacher的更多相关文章

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

    题意 给定 \(n\) 个字符串,\(m\) 个询问,每次询问 \(a\) 字符串的后缀和 \(b\) 字符串的前缀最多能匹配多长. \(1\leq n,m \leq 10^5\) 思路 多串匹配,考 ...

  2. hdu 5894 hannnnah_j’s Biological Test 组合数学

    传送门:hdu 5894 hannnnah_j’s Biological Test 题目大意:n个座位,m个学生,使每个学生的间隔至少为k个座位 组合中的插空法 思路:每个学生先去掉k个空位间隔,剩下 ...

  3. HDU 5066 Harry And Physical Teacher(物理题)

    HDU 5066 Harry And Physical Teacher 思路:利用物理里面的动量守恒公式.因为保证小车质量远大于小球.所以能够把小车质量当成无穷大带进去,得到答案为2 * v0 - v ...

  4. HDU 5059 Help him(细节)

    HDU 5059 Help him 题目链接 直接用字符串去比較就可以,先推断原数字正确不对,然后写一个推断函数,注意细节,然后注意判掉空串情况 代码: #include <cstdio> ...

  5. HDU 5894 hannnnah_j’s Biological Test【组合数学】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5894 题意: 一个圆桌上有$n$个不同的位置,$m$个相同的人安排到这$n$个位置上,要求两人相邻的 ...

  6. BestCoder12 1002.Help him(hdu 5059) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否 ...

  7. hdu 5059 简单字符串处理

    http://acm.hdu.edu.cn/showproblem.php?pid=5059 确定输入的数是否在(a,b)内 简单字符串处理 #include <cstdio> #incl ...

  8. HDU 5059 Help him(简单模拟题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目大意: 给定一个字符串,如果这个字符串是一个整数,并且这个整数在[a,b]的范围之内(包括a,b),那 ...

  9. 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 (Ja ...

随机推荐

  1. [div+css]网站布局实例二

    重点: 合理应用"xhtml标签"建立良好的页面结构 拿到一份"设计方案"的效果图后不要立即开始编码,而是要 首先理清"各元素之间的关系"; ...

  2. OpenStack collectd的从零安装服务端

    安装collectd包操作同客户端相同,不在赘述 配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...

  3. scala抽象类抽象字段

    package com.test.scala.test /** * 抽象类学习,定义abstact关键字 */ abstract class AbstractClass { val id:Int;// ...

  4. Hibernate解决n+1问题

    观点:对于n+1问题的理解. 一般而言说n+1意思是,无论在一对多还是多对一当查询出n条数据之后,每条数据会关联的查询1次他的关联对象,这就叫做n+1. 但是我的理解是,本来所有信息可以一次性查询出来 ...

  5. win10 python nltk安装

    主要是参照http://www.tuicool.com/articles/VFf6Bza

  6. JDK结构介绍

    dt.jar和tools.jar位于:{Java_Home}/lib/下, 而rt.jar位于:{Java_Home}/jre/lib/下, 其中: (1) rt.jar是JAVA基础类库,也就是你在 ...

  7. javaScript数组循环删除

    遍历数组循环的时候,限定条件不要写arr.length,因为数组的长度会随着删除元素的同时减小. 例如,一个原本长度为10的数组,如果采用 for(var i = 0; i< arr.lengt ...

  8. 杭电1002-A + B Problem II

    #include<stdio.h>#include<string.h> int main(){    char str1[1001],str2[1001];    int t, ...

  9. [转]ios 开发file's owner以及outlet与连线的理解

    转载地址:http://www.cocoachina.com/bbs/simple/?t108822.html xib文件本身可以看做是一个xml,app启动的时候会根据xml构造xib对应的界面及其 ...

  10. [转] Xcode4.4.1下安装高德地图详细教程

    转载地址:http://blog.csdn.net/mad1989/article/details/7913404 此教程和官方的没有太大区别,省略了好多没用的步骤,添加framework的方式是最新 ...