博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
阅读量:6591 次
发布时间:2019-06-24

本文共 2182 字,大约阅读时间需要 7 分钟。

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 
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
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

 

转载地址:http://ktuio.baihongyu.com/

你可能感兴趣的文章
Dell PowerEdge R940解析:四路顶配服务器维护平民化
查看>>
《数据重现》赠书活动开始
查看>>
Android 中文 API (93) —— BaseExpandableListAdapter
查看>>
PowerShell中单引号和双引号的区别
查看>>
【ARM】gpio·arm体系结构之gpio
查看>>
数据库启动时遇到ORA-01578错误
查看>>
Fedora 12 (Constantine)Beta版及Alpha镜像下载
查看>>
稳扎稳打Silverlight(3) - 2.0控件之Border, Button, Calendar, Canvas, CheckBox, ComboBox
查看>>
Who's Using Cyberthreat Intelligence and How?
查看>>
【翻译】SQL Server索引进阶:第六级,标签
查看>>
确定两串乱序同构
查看>>
寒假研究计划
查看>>
Android:ListView、BaseAdapter、convertView、ListView优化及事件、notifyDataSetChanged()
查看>>
新版发布功能上线,新增「大屏快照」功能!
查看>>
代码调优及其他zz
查看>>
Centos7+Postfix+Dovecot实现邮件收发
查看>>
“蒜你狠”和“豆你玩”的遐想。。
查看>>
无法解析连接描述中指定的SID
查看>>
ext3格式化成ext4
查看>>
自己编译redhat 9.0内核心得
查看>>