hdu 5918 Harmonic Value Description 2016ACM/CCPC长春赛区现场赛H

news/2024/7/11 5:01:27 标签: 贪心

Problem Description
Mr. Frog has two sequences  a1,a2,,an  and  b1,b2,,bm  and a number p. He wants to know the number of positions q such that sequence  b1,b2,,bm  is exactly the sequence  aq,aq+p,aq+2p,,aq+(m1)p  where  q+(m1)pn  and  q1 .
 

Input
The first line contains only one integer  T100 , which indicates the number of test cases.

Each test case contains three lines.

The first line contains three space-separated integers  1n106,1m106  and  1p106 .

The second line contains n integers  a1,a2,,an(1ai109) .

the third line contains m integers  b1,b2,,bm(1bi109) .
 

Output
For each test case, output one line “Case #x: y”, where x is the case number (starting from 1) and y is the number of valid q’s.
 

Sample Input
  
2 6 3 1 1 2 3 1 2 3 1 2 3 6 3 2 1 3 2 2 3 1 1 2 3
 

Sample Output
  
Case #1: 2 Case #2: 1


将a数列的元素按照对p取模的值重新排列,构成新的数列,不同取模值之间加上一个-1作为分界

然后直接跑KMP即可

#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int a[200001],aa[100001],b[100001];
int loc[100001],net[100001];
int n,m;
inline void KMP()
{
	int i,j;
	for(i=1;i<m;i++)
	{
		j=i;
		while(j>0)
		{
			j=net[j];
			if(b[j]==b[i])
			{
				net[i+1]=j+1;
				break;
			}
		}
	}
	for(i=0,j=0;i<n;i++)
	{
		if(j<m&&a[i]==b[j])
			j++;
		else
		{
			while(j>0)
			{
				j=net[j];
				if(a[i]==b[j])
				{
					j++;
					break;
				}
			}
		}
		if(j==m)
		{
			loc[0]++;
			loc[loc[0]]=(i-m+1)+1;
		}
	}
}
int main()
{
	int T,k=0;
	scanf("%d",&T);
	while(T>0)
	{
		T--;
		k++;
		int p;
		scanf("%d%d%d",&n,&m,&p);
		int i,j;
		for(i=0;i<n;i++)
			scanf("%d",&aa[i]);
		for(i=0;i<m;i++)
			scanf("%d",&b[i]);
		int tot=-1;
		for(i=0;i<p;i++)
		{
			for(j=i;j<n;j+=p)
			{
				tot++;
				a[tot]=aa[j];
			}
			tot++;
			a[tot]=-1;
		}
	//	for(i=0;i<=tot;i++)
	//		printf("%d ",a[i]);
	//	printf("\n");
		memset(net,0,sizeof(net));
		loc[0]=0;
		n=tot+1;
		KMP();
		printf("Case #%d: %d\n",k,loc[0]);
	}
	return 0;
}



http://www.niftyadmin.cn/n/942615.html

相关文章

GDOI 2016 Day1 T1 中学生数学题

Description 已知一种商品的购买人数n和价格p之间的关系是n⌊n0−pk⌋&#xff0c;收益是n0(p−p0)。 或者有两种商品&#xff0c;购买第一种商品的人数n1和价格p1满足n1⌊n0−p1k⌋&#xff0c;第二种商品的人数n2和价格p2满足n2⌊n0−p2k⌋−n1&#xff0c;其总收益为n1(p1…

诉苦!first

长期连载…… 这星期考了3次试……小测验 感觉人生无望啊啊啊&#xff0c; 诉个苦…… day1&#xff1a; 分数180…… 第一题幸好会&#xff0c;&#xff0c;第二题&#xff01; 第二题提醒大家把&#xff0c;&#xff0c;&#xff0c;就是模数10^18&#xff0c;然后~~要…

GDOI 2016 Day1 T2 最长公共子串

Description 给出两个字符串A和B&#xff0c;求最长公共子串。 其中B串中有k个区间的字符可以任意调换。 |A|,|B|<2000,k<100000 Solution 首先&#xff0c;一个很明显的性质&#xff0c;两个区间如果有交集&#xff0c;那么这两个区间可以合并成一个。 然后&#…

BZOJ 1878 [SDOI2009]HH的项链 莫队

Description HH有一串由各种漂亮的贝壳组成的项链。HH相信不同的贝壳会带来好运&#xff0c;所以每次散步 完后&#xff0c;他都会随意取出一段贝壳&#xff0c;思考它们所表达的含义。HH不断地收集新的贝壳&#xff0c;因此他的项链变得越来越长。有一天&#xff0c;他突然提出…

bzoj 4320: ShangHai2006 Homework

Description 1&#xff1a;在人物集合 S 中加入一个新的程序员&#xff0c;其代号为 X,保证 X 在当前集合中不存在。 2&#xff1a;在当前的人物集合中询问程序员的mod Y 最小的值。 &#xff08;为什么统计这个&#xff1f;因为拯救过世界的人太多了&#xff0c;只能取模&…

GDOI 2016 Day1 T4 疯狂动物城

Description 给出一个N个节点的数&#xff0c;和M次操作。每次操作的类型如下&#xff1a; 1,x,y,z&#xff0c;将x到y的路径上的ai加上z 2,x,y&#xff0c;询问x到y的路径上&#xff0c;ai*(12..n-i)的和 3,x&#xff0c;将所有的a变更回第x次修改之后的状态。 强制在线。…

BZOJ 3211 花神游历各国 线段树

Description Input Output 每次x1时&#xff0c;每行一个整数&#xff0c;表示这次旅行的开心度 Sample Input 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 2 3 1 1 4 Sample Output 101 11 11 HINT 对于100%的数据&#xff0c; n ≤ 100000&#xff0c;m≤200000 ,data[i]非负…

Codeforces Round Intel Code Challenge Final Round A.Checking the Calendar

题目大意&#xff1a;问你是否存在一个非闰年&#xff0c;使得某个月的开始为第一行所给的星期&#xff0c;下一个月开始为第二行所给的星期 直接模拟即可。 或者因为每个月天数固定&#xff0c;所以只需要考虑特定的几种情况 #include<set> #include<map> #inclu…