zoukankan      html  css  js  c++  java
  • HDU 17111 Number Sequence(KMP裸题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711

    题目大意:给你两个数字数组a和b,若b是a的子序列则输出b在a中第一次出现的位置,否则输出-1。
    解题思路:直接套模板即可。

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 const int N=1e6+5;
     7 
     8 int n,m;
     9 int nxt[N],p[N],s[N];
    10 
    11 void getnext(){
    12     int i,j;
    13     j=nxt[0]=-1;
    14     i=0;
    15     while(i<m){
    16         while(j!=-1&&p[i]!=p[j])
    17             j=nxt[j];
    18         nxt[++i]=++j;
    19     }
    20 }
    21 
    22 int kmp(){
    23     getnext();
    24     int i=0,j=0;
    25     while(i<n){
    26         while(j!=-1&&s[i]!=p[j])
    27             j=nxt[j];
    28         i++,j++;
    29         if(j==m)
    30             return i-m+1;
    31     }
    32     return -1;
    33 }
    34 
    35 int main(){
    36     ios::sync_with_stdio(false);
    37     int t;
    38     cin>>t;
    39     while(t--){
    40         cin>>n>>m;
    41         for(int i=0;i<n;i++){
    42             cin>>s[i];
    43         }
    44         for(int i=0;i<m;i++){
    45             cin>>p[i];
    46         }
    47         cout<<kmp()<<endl;
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    python的包和模块
    python 匿名函数
    hdu 1455 Sticks
    python 返回函数
    python 自定义排序函数
    batchsize对收敛速度的影响
    mini_batch GD
    dropout
    sift
    hog
  • 原文地址:https://www.cnblogs.com/fu3638/p/8486446.html
Copyright © 2011-2022 走看看