HDU1711 Number Sequence KMP

欢迎访问~原文出处——博客园-zhouzhendong

去博客园看该题解


题目传送门 - HDU1711


题意概括

  给T组数据,每组有长度为n和m的母串和模式串。判断模式串是否是母串的子串,如果是输出最先匹配完成的位置,否则输出-1.


题解

  KMP裸题。


代码

#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
const int N=1000005,M=10005;
bool isd(char ch){
	return ‘0‘<=ch&&ch<=‘9‘;
}
void read(int &x){
	int f=1;
	x=0;
	char ch=getchar();
	while (!isd(ch)&&ch!=‘-‘)
		ch=getchar();
	if (ch==‘-‘)
		f=-1,ch=getchar();
	while (isd(ch))
		x=x*10+ch-48,ch=getchar();
	x*=f;
}
int T,n,m,a[N],b[M],Next[M];
int main(){
	read(T);
	while (T--){
		read(n),read(m);
		for (int i=0;i<n;i++)
			read(a[i]);
		for (int i=0;i<m;i++)
			read(b[i]);
		memset(Next,0,sizeof Next);
		int k=0;
		for (int i=1;i<m;i++){
			while (k&&b[k]!=b[i])
				k=Next[k-1];
			if (b[k]==b[i])
				k++;
			Next[i]=k;
		}
		int ans=-1;
		k=0;
		for (int i=0;i<n;i++){
			while (k&&b[k]!=a[i])
				k=Next[k-1];
			if (b[k]==a[i])
				k++;
			if (k==m){
				ans=i-m+2;
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

  

时间: 2024-10-08 14:14:32

HDU1711 Number Sequence KMP的相关文章

hdu1711 Number Sequence kmp应用

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目: 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 m

hdu 1711 Number Sequence(KMP)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int n,m,next[10010],a[1000010],b[10010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<m) { if(j==-1||b[i]==b[j]) i++,j++,next[i]=j; else j=next[

HDU 1711 Number Sequence KMP题解

KMP查找整数数列,不是查找字符串. 原理是一样的,不过把字符串转换为数列,其他基本上是一样的. #include <stdio.h> #include <string.h> const int MAX_N = 1000001; const int MAX_M = 10001; int strN[MAX_N], strM[MAX_M], next[MAX_M], N, M; void getNext() { memset(next, 0, sizeof(int) * M); for

HDU 1711 Number Sequence(KMP算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15548    Accepted Submission(s): 6836 Problem Description Given two sequence

Problem A Number Sequence(KMP基础)

A - Number Sequence Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1711 Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 100

hdu 1711 Number Sequence KMP 基础题

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11691    Accepted Submission(s): 5336 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1],

(KMP 1.1)hdu 1711 Number Sequence(KMP的简单应用——求pattern在text中第一次出现的位置)

题目: Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12902    Accepted Submission(s): 5845 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1

HDU - 1711 Number Sequence KMP字符串匹配

HDU - 1711 Number Sequence Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <=

kuangbin专题十六 KMP&amp;&amp;扩展KMP HDU1711 Number Sequence

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