hdu——1711Number Sequence(kmp专练)

本文介绍了一种利用KMP算法解决序列匹配问题的方法。针对两个整数序列,任务是找到一个起始位置,使得第一个序列中的一段连续子序列与第二个序列完全相同。文章通过具体的示例和代码详细解释了算法的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Number Sequence
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19556    Accepted Submission(s): 8389


Problem Description


Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.



Input


The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].



Output


For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.




Sample Input


2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1


Sample Output


6
-1


Source


 HDU 2007-Spring Programming Contest  


Recommend


lcy



可以用单个数字输入抵消空格的影响 不直接用string 进行kmp算法


#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int nexta[4000100];
int a[1000100],b[1000010];//a是子串b是模板 
void getnext(int a[],long long h)
{
	int i=0,j=-1;
	nexta[0]=-1;
	while(i<h)
	{
		if(j==-1||a[i]==a[j])
		{
			++i;
			++j;
			nexta[i]=j;
		}
		else j=nexta[j];
	}
}
long long kmp(int a[],long long h,int b[],long long k)
{
	int i=0,j=0;
	while(i<k)
	{
		if(a[j]==b[i]||j==-1)
		{
			++i;
			++j;
		}
		else j=nexta[j];
		if(j==h)
		{
			return i-h+1;//从0开始的
		}
	}
	return -1;
}
int main()
{
	long long h,k,i,n;
	ios::sync_with_stdio(false);
	cin>>n;
	while(n--)
	{
		cin>>k>>h;
		for(i=0;i<k;i++)
		cin>>b[i];
		for(i=0;i<h;i++)
		cin>>a[i];
		getnext(a,h);
		cout<<kmp(a,h,b,k)<<endl;
	}
	return 0;
}

### HDU OJ Problem 2566 Coin Counting Solution Using Simple Enumeration and Generating Function Algorithm #### 使用简单枚举求解硬币计数问题 对于简单的枚举方法,可以通过遍历所有可能的组合方式来计算给定面额下的不同硬币组合数量。这种方法虽然直观但效率较低,在处理较大数值时性能不佳。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] coins = {1, 2, 5}; // 定义可用的硬币种类 while (scanner.hasNext()) { int targetAmount = scanner.nextInt(); int countWays = findNumberOfCombinations(targetAmount, coins); System.out.println(countWays); } } private static int findNumberOfCombinations(int amount, int[] denominations) { if (amount == 0) return 1; if (amount < 0 || denominations.length == 0) return 0; // 不使用当前面值的情况 int excludeCurrentDenomination = findNumberOfCombinations(amount, subArray(denominations)); // 使用当前面值的情况 int includeCurrentDenomination = findNumberOfCombinations(amount - denominations[0], denominations); return excludeCurrentDenomination + includeCurrentDenomination; } private static int[] subArray(int[] array) { if (array.length <= 1) return new int[]{}; return java.util.Arrays.copyOfRange(array, 1, array.length); } } ``` 此代码实现了通过递归来穷尽每一种可能性并累加结果的方式找到满足条件的不同组合数目[^2]。 #### 利用母函数解决硬币计数问题 根据定义,可以将离散序列中的每一个元素映射到幂级数的一个项上,并利用这些多项式的乘积表示不同的组合情况。具体来说: 设 \( f(x)=\sum_{i=0}^{+\infty}{a_i*x^i}\),其中\( a_i \)代表当总金额为 i 时能够组成的方案总数,则有如下表达式: \[f_1(x)=(1+x+x^2+...)\] 这实际上是一个几何级数,其封闭形式可写作: \[f_1(x)=\frac{1}{(1-x)}\] 同理,对于其他类型的硬币也存在类似的生成函数。因此整个系统的生成函数就是各个单独部分之积: \[F(x)=f_1(x)*f_2(x)...*f_n(x)\] 最终目标是从 F(x) 中提取系数即得到所需的结果。下面给出基于上述理论的具体实现: ```cpp #include<iostream> using namespace std; const int MAXN = 1e4 + 5; int dp[MAXN]; void solve() { memset(dp, 0, sizeof(dp)); dp[0] = 1; // 初始化基础状态 int values[] = {1, 2, 5}, size = 3; for (int j = 0; j < size; ++j){ for (int k = values[j]; k <= 10000; ++k){ dp[k] += dp[k-values[j]]; } } } int main(){ solve(); int T; cin >> T; while(T--){ int n; cin>>n; cout<<dp[n]<<endl; } return 0; } ``` 这段 C++ 程序展示了如何应用动态规划技巧以及生成函数的概念高效地解决问题实例[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值