链接: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. Scrum站立会议----11月4日

    小组名称:nice! 组长:李权 成员:于淼  刘芳芳韩媛媛 宫丽君 时间:2016.11.4    12:00--12:30 地点:传媒西楼220室 代码地址:Https:  https://git ...

  2. 记在centos中连接无线网络的一次过程

    1. 首先, 你的系统要能驱动无限网卡, 要是人品好的话, 系统已经自带了你的网卡的驱动程序. 不然就要先搞定无线网卡的驱动再说. 不然后面的步骤也就没必要了. 2. 看一下你的无线网卡叫什么: iw ...

  3. Job类

    当我们在写MapReduce程序的时候,通常,在main函数里,我们会像下面这样做.建立一个Job对象,设置它的JobName,然后配置输入输出路径,设置我们的Mapper类和Reducer类,设置I ...

  4. sql server 2008 安装过程与创建建sql server登录用户

    1.sql server 下载安装包路径:http://pan.baidu.com/s/1qWuzddq 2.安装过程图解教程 ,参照网址:http://jingyan.baidu.com/album ...

  5. android 开发工具(android studio)

      Android Studio 从安装到配置使用 okhttp比xutils功能强大,源码地址: https://github.com/search?utf8=✓&q=okhttp andr ...

  6. android 项目学习随笔十二(ListView加脚布局)

    1.ListView加脚布局 头布局initHeaderView,在onTouchEvent事件中进行显示隐藏头布局切换 脚布局initFooterView,实现接口OnScrollListener, ...

  7. scrapy学习记录

    scrapy是一个用来爬取一个或多个网站的数据,提取数据的应用框架.下载过程非常复杂,而且会遇到各种问题.所以写个博客来记录下. 安装好python2.7之后,就可以开始.安装scrapy前还需要安装 ...

  8. JSP中文乱码问题解决方法小结

    在使用JSP的过程中,最使人头疼的一个问题就是中文乱码问题,以下是我在软件开发中遇到的乱 码问题以及解决方法. 1.JSP页面乱码 这种乱码的原因是应为没有在页面里指定使用的字符集编码,解决方法:只要 ...

  9. c/c++ 常用的几个安全函数

    _stprintf_s  // 格式化字符串 _vsntprintf_s // 格式化 不定长参数用到

  10. oracle 11g升级

    手先需要下载oracle11g的补丁安装包,一般只有付费用户才能获取到该安装包,其他用户获取不到,那怎么办呢? 万能的互联网可以办到,可以用http://www.wangpansou.cn/所有其他用 ...