#include <stdio.h>
int main(){
int a[1001]={0};
int i,j,len,len2;
for (i=0;i<=1000;i++){
scanf("%d",&a[i]);
if(a[i]==-1){
break;
}
}
int b[i];
if(a[i]==-1){
for (j=0;j<i;j++){
b[j]=a[j];
}
len=i;
}
int c[1001];
for (i=0;i<=1000;i++){
scanf("%d",&c[i]);
if (c[i]==-1){
break;
}
}
int d[i];
if (c[i]==-1){
for (j=0;j<i;j++){
d[j]=c[j];
}
len2=i;
}
if(len2>len) {printf("ListB is not the sub sequence of ListA.");
return 0;}
for (j=0;j<=len-1;j++){
if(d[0]==b[j])
{
if(len2==1) {printf("ListB is the sub sequence of ListA.");
return 0;}
for(i=1;d[i]==b[j+1];i++)
{
if(j==len-2&&i<len2-1) {printf("ListB is not the sub sequence of ListA.");
return 0;}
else if(i==len2-1&&j<len-2) {printf("ListB is the sub sequence of ListA.");
return 0;}
else if(j==len-2&&i==len2-1) {printf("ListB is the sub sequence of ListA.");
return 0;}
else j++;
}
}
}
printf("ListB is not the sub sequence of ListA.");
return 0;
}
已知两个由正整数组成的无序序列A、B,每个序列的元素个数未知,但至少有一个元素。你的任务是判断序列B是否是序列A的连续子序列。假设B是“1 9 2 4 18”,A是“33 64 1 9 2 4 18 7”,B是A的连续子序列;假设B是“1 9 2 4 18”,A是“33 1 9 64 2 4 18 7”,B不是A的连续子序列。
输入格式:
依次输入两个乱序的正整数序列A、B,序列中元素个数未知,但每个序列至少有一个元素,并以输入“-1”结束(-1不算序列中元素),每个序列占一行。两个数列的长度均不超过1000。
输入保证合法,且所有整数均可用int存储。
输出格式:
如果序列B是序列A的连续子序列,则输出“ListB is the sub sequence of ListA.”,否则输出“ListB is not the sub sequence of ListA.”。
输入样例:
5 4 3 2 1 -1
3 2 1 -1
输出样例:
ListB is the sub sequence of ListA.
pta上差一个测试点没过,可能逻辑上有差池?