8.11 I
来源:互联网 发布:淘宝手机直通车怎么找 编辑:程序博客网 时间:2024/05/29 18:51
I - Number Sequence
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, outputthe smallest one.
Input
The first line of input is a number T which indicate thenumber of cases. Each case contains three lines. The first line is two numbersN and M (1 <= M <= 10000, 1 <= N <= 1000000). The second linecontains N integers which indicate a[1], a[2], ...... , a[N]. The third linecontains M integers which indicate b[1], b[2], ...... , b[M]. All integers arein the range of [-1000000, 1000000].
Output
For each test case, you should output one line which onlycontain 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
题意:输入一个数字t,有t组数据,对于每一组数据输入两个数字m,n,然后输入两个长度为m,n的字符串,判断第二个字符串是否在第一个字符串中出现过,如果出现过,输出起始点下标,如果没有出现,输出-1。
思路:用next数组比较,对于每一次比较的情况存入extend数组,然后判断extend数组中是否含有与第二个字符串相等的数字,如果有,则将该点下标减去第二个字符串长度输出。也可以直接在next数组中比较。
#include<stdio.h>#include<string.h>int s1[1110000],s2[1110000],next[1110000],extend[1110000];int main(){int i,j,k,T,m,n,max,f;while(scanf("%d",&T)!=EOF){while(T--){scanf("%d%d",&m,&n);memset(next,0,sizeof(next));memset(extend,0,sizeof(extend));for(i=0;i<m;i++){scanf("%d",&s1[i]);}for(j=0;j<n;j++){scanf("%d",&s2[j]);}j=0;for(i=1;i<n;)//计算next数组{if(s2[i]==s2[j]){next[i]=j+1;i++;j++;}else if(j==0&&s2[i]!=s2[j]){i++;}else if(j>0&&s2[i]!=s2[j]){j=next[j-1];}}/*for(i=0;i<n;i++){printf("%d ",next[i]);}printf("\n");*/i=0;j=0;while(i<m&&j<n){if(s1[i]==s2[j]){extend[i]=j+1;i++;j++;}else if(j==0&&s1[i]!=s2[j]){extend[i]=j;i++;}else if(i>0&&s1[i]!=s2[j]){j=next[j-1];extend[i]=0;}}/*for(i=0;i<m;i++){printf("%d ",extend[i]);}printf("\n");*/max=0;for(i=0;i<m;i++){if(extend[i]==n){f=i;//记录位置max=1;//记录是否完全重合break;}}if(max==1){printf("%d\n",f-n+2);//因为字符串是从0开始输出的是重合时的第一位所以加上2}else{printf("-1\n");}}}return 0;}
- 8.11 I
- I
- I
- ++i
- I
- I
- i
- ++i
- i++
- (++i) + (++i) + (++i) + (++i)
- i++
- i
- i
- I++,I--,++I,--I
- I
- i++
- i++
- I
- CentOS 7配置MySQL
- VOC-xml标注文件制作及其交互使用(修改,预览,存储)的MATLAB实现
- js中的基本数据类型
- hdu 4763 Theme Section
- TxQueryRunner用法
- 8.11 I
- 超级牛逼的线段树总结 啊哈哈~~~
- IntelliJ IDEA 2017 免费激活方法
- 小程序获取用户信息方式更新了!!
- HDU 6103 Kirinriki
- sphinx 的安装与应用
- 自定义注解框架的那些事
- WordPress需要删除的13个函数
- 客户端调用CXF2.2.3 webservice