HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

简介: Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27028    Accepted Submission...

Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27028    Accepted Submission(s): 11408


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裸题,自己看吧,不会的看我博客详解!此题有道坑点就是读入不能用cin读入,很容易T!

纯粹要看运气才会过QAQ

优化以后:

 

 速度快了将近3.5s,scanf大法好啊

下面给出AC代码:
 
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 const int N=1000050;
  4 inline int read()
  5 {
  6     int x=0,f=1;
  7     char ch=getchar();
  8     while(ch<'0'||ch>'9')
  9     {
 10         if(ch=='-')
 11             f=-1;
 12         ch=getchar();
 13     }
 14     while(ch>='0'&&ch<='9')
 15     {
 16         x=x*10+ch-'0';
 17         ch=getchar();
 18     }
 19     return x*f;
 20 }
 21 int kmpnext[N];
 22 int s[N],t[N];///s为主串,t为模式串
 23 int slen,tlen;///slen为主串的长度,tlen为模式串的长度
 24 inline void getnext()
 25 {
 26     int i,j;
 27     j=kmpnext[0]=-1;
 28     i=0;
 29     while(i<tlen)
 30     {
 31         if(j==-1||t[i]==t[j])
 32         {
 33             kmpnext[++i]=++j;
 34         }
 35         else
 36         {
 37             j=kmpnext[j];
 38         }
 39     }
 40 }
 41 /*
 42 返回模式串T在主串S中首次出现的位置
 43 返回的位置是从0开始的。
 44 */
 45 inline int kmp_index()
 46 {
 47     int i=0,j=0;
 48     getnext();
 49     while(i<slen&&j<tlen)
 50     {
 51         if(j==-1||s[i]==t[j])
 52         {
 53             i++;
 54             j++;
 55         }
 56         else
 57             j=kmpnext[j];
 58     }
 59     if(j==tlen)
 60         return i-tlen;
 61     else
 62         return -1;
 63 }
 64 /*
 65 返回模式串在主串S中出现的次数
 66 */
 67 inline int kmp_count()
 68 {
 69     int ans=0;
 70     int i,j=0;
 71     if(slen==1&&tlen==1)
 72     {
 73         if(s[0]==t[0])
 74             return 1;
 75         else
 76             return 0;
 77     }
 78     getnext();
 79     for(i=0;i<slen;i++)
 80     {
 81         while(j>0&&s[i]!=t[j])
 82             j=kmpnext[j];
 83         if(s[i]==t[j])
 84             j++;
 85         if(j==tlen)
 86         {
 87             ans++;
 88             j=kmpnext[j];
 89         }
 90     }
 91     return ans;
 92 }
 93 int T;
 94 int main()
 95 {
 96     T=read();
 97     while(T--)
 98     {
 99         slen=read();
100         tlen=read();
101         for(int i=0;i<slen;i++)
102             s[i]=read();
103         for(int i=0;i<tlen;i++)
104             t[i]=read();
105         if(kmp_index()==-1)
106             cout<<-1<<endl;
107         else
108             cout<<kmp_index()+1<<endl;
109     }
110     return 0;
111 }

 

目录
相关文章
|
存储 缓存 关系型数据库
Redo日志 (4)—log sequence number(六十二)
Redo日志 (4)—log sequence number(六十二)
|
Java C++
HDU-1005,Number Sequence(有规律的数学题)
HDU-1005,Number Sequence(有规律的数学题)
|
人工智能 Java
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
133 0
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
117 0
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
118 0
HDOJ 1005 Number Sequence
HDOJ 1005 Number Sequence
100 0
|
人工智能 Java Windows
枚举 + 进制转换 --- hdu 4937 Lucky Number
Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 294    Accepted Submission(s):...
834 0
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
99 1

热门文章

最新文章