zoukankan      html  css  js  c++  java
  • HDU 1711 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): 30591    Accepted Submission(s): 12870


    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
         把数字离散化后当作字符来处理就是kmp了,注意模板是从下标0开始的我一直输入时从1开始导至答案不对。
       
     1 #include<cstring>
     2 #include<cstdio>
     3 #include<map>
     4 #include<iostream>
     5 using namespace std;
     6 map<int,int>M;
     7 int nex[10005],l1,l2,sl;
     8 int S[1000005],T[10005];
     9 int solve()
    10 {
    11     int i,j;
    12     nex[0]=nex[1]=0;
    13     for(i=1;i<l2;++i)
    14     {
    15         j=nex[i];
    16         while(j&&T[i]!=T[j])j=nex[j];
    17         nex[i+1]=T[i]==T[j]?j+1:0;
    18     }
    19     j=0;
    20     for(i=0;i<l1;++i)
    21     {
    22         while(j&&S[i]!=T[j])j=nex[j];
    23         if(S[i]==T[j]){
    24             j++;
    25             if(j==l2)return i-l2+2;
    26         }
    27     }
    28     return -1;
    29 }
    30 int main()
    31 {
    32     int i,j,t,p,x,tmp;
    33     cin>>t;
    34     while(t--){p=0;M.clear();
    35         scanf("%d%d",&l1,&l2);
    36         for(i=0;i<l1;++i)
    37         {
    38             scanf("%d",&x);
    39             tmp=M[x];
    40             if(!tmp){M[x]=S[i]=++p;}
    41             else S[i]=tmp;
    42         }
    43         for(i=0;i<l2;++i)
    44         {
    45             scanf("%d",&x);
    46             tmp=M[x];
    47             if(!tmp){M[x]=T[i]=++p;}
    48             else T[i]=tmp;
    49         }
    50         cout<<solve()<<endl;;
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    NET5 WebApi 解决跨域问题
    CentOS7安装MYSQL
    VMWare安装CentOS7
    MSSQL还原数据库,更改用户登陆权限
    Vue自定义页面路由
    解决VSCODE"因为在此系统上禁止运行脚本"报错
    简析 HTTP 2.0 多路复用
    Git放弃本地修改,强制拉取最新版
    eclipse安装OpenExplorer插件--快速打开文件目录
    git统计某段时间内代码的修改量/总代码量
  • 原文地址:https://www.cnblogs.com/zzqc/p/7588496.html
Copyright © 2011-2022 走看看