hdu 1711 Number Sequence

点击打开链接hdu1711

思路:KMP

kmp算法:
1 kmp是用来匹配字符串,只能够匹配单一的字符串
2 kmp的算法的过程:
  1:假设文本串的长度为n,模式串的长度为m;
  2:先例用O(m)的时间去预处理next数组,next数组的意思指的是当前的字符串匹配失败后要转到的下一个状态;
  3:利用o(n)的时间去完成匹配;

3 时间复杂度为o(n+m)即o(n);

代码:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

#define MAXN 1000010

int t , n , m;
int text[MAXN];/*文本串*/
int pattern[MAXN];/*模式串*/
int next[MAXN];/*next数组*/

/*O(m)的时间求next数组*/
void getNext(){
    next[0] = next[1] = 0;
    for(int i = 1 ; i < m ; i++){
       int j = next[i];
       while(j && pattern[i] != pattern[j])
          j = next[j];
       next[i+1] = pattern[i] == pattern[j] ? j+1 : 0;
    }
}

/*o(n)的时间进行匹配*/
void find(){
    int j = 0;/*初始化在模式串的第一个位置*/
    for(int i = 0 ; i < n ; i++){/*遍历整个文本串*/
       while(j && pattern[j] != text[i])/*顺着失配边走,直到可以匹配,最坏得到情况是j = 0*/
         j = next[j];
       if(pattern[j] == text[i])/*如果匹配成功继续下一个位置*/
         j++;
       if(j == m){/*如果找到了直接输出*/
         printf("%d\n" , i-m+2);/*输出在文本串中第一个匹配的位置,不是下标*/
         return;
       }
    }
    printf("-1\n");
}

int main(){
   scanf("%d" , &t);
   while(t--){
      scanf("%d%d" , &n , &m);
      for(int i = 0 ; i < n ; i++)
         scanf("%d" , &text[i]);
      for(int i = 0 ; i < m ; i++)
         scanf("%d" , &pattern[i]);
      getNext();
      find();
   }
   return 0;
}
时间: 2024-10-19 03:50:48

hdu 1711 Number Sequence的相关文章

UVa 10706:Number Sequence

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1647 原题: A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of n

UVa 10706 / POJ 1019 Number Sequence:打表及O(1)算法

10706 - Number Sequence Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1647 http://poj.org/problem?id=1019 Description A single positive integer i is giv

acm-杭电ACM OJ 1005 Number Sequence

问题描述 杭电ACM OJ 1005 Number Sequence A number sequence is defined as follows: f(1) = 1 f(2) = 1 f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A B and n you are to calculate the value of f(n). 超过49个数之后一定会出现和之前的数组合相同的情况,这个我可以了解,但是 为什么最多经过49个数之后一定会出现周

[ACMcoder] Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

HDOJ 1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

HDU 1711

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

hdu 2454 Degree Sequence of Graph G

点击打开链接 Degree Sequence of Graph G Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1997    Accepted Submission(s): 850 Problem Description Wang Haiyang is a strong and optimistic Chinese youngst

hdu 2062 Subset sequence【有点康拓展开的意思】

Subset sequence Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3441    Accepted Submission(s): 1740 Problem Description Consider the aggregate An= { 1, 2, -, n }. For example, A1={1}, A3={1,2,

hdu 5400 Arithmetic Sequence

click here~~ ***Arithmetic Sequence*** Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence if and only if there exist i(1≤i≤n) such that for every j(1≤j<i),bj+1=bj+d1 and for every j(i≤j<n),bj+1=bj+d2. Teacher Mai has a