测试链接
总时间限制: 1000ms 内存限制: 65536kB
描述
有三个字符串S,S1,S2,其中,S长度不超过300,S1和S2的长度不超过10。想检测S1和S2是否同时在S中出现,且S1位于S2的左边,并在S中互不交叉(即,S1的右边界点在S2的左边界点的左侧)。计算满足上述条件的最大跨距(即,最大间隔距离:最右边的S2的起始点与最左边的S1的终止点之间的字符数目)。如果没有满足条件的S1,S2存在,则输出-1。
例如,S = “abcd123ab888efghij45ef67kl”, S1=“ab”, S2=“ef”,其中,S1在S中出现了2次,S2也在S中出现了2次,最大跨距为:18。
输入
三个串:S1, S2, S3,其间以逗号间隔(注意,S1, S2, S3中均不含逗号和空格);
输出
S1和S2在S最大跨距;若在S中没有满足条件的S1和S2,则输出-1。
样例输入
abcd123ab888efghij45ef67kl,ab,ef
样例输出
18
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[305],s1[15],s2[15];
int main()
{
char c;
int len=0,len1=0,len2=0,start,end,k;
while((c=getchar())&&c!=',')
s[len++]=c;//输入字符数组s
while((c=getchar())&&c!=',')
s1[len1++]=c;//输入字符数组s1
while((c=getchar())&&c!=EOF)
s2[len2++]=c;//输入字符数组s2
int i,j=0,ans=0;
for(i=0;i<len;i++)
{
if(s[i]==s1[j])
j++;
else
{
j=0;
if(s[i]==s1[j])
j++;
}
if(j==len1)
{
start=i+1;
break;
}
}
k=len2-2;//后面的字符串有截至字符所以要减2
for(i=len-1;i>=0;i--)
{
if(s[i]==s2[k])
k--;
else
{
k=len2-2;
if(s[i]==s2[k])
k--;
}
if(k==0)
{
end=i-1;
break;
}
}
if(end-start>=0)
cout<<end-start;
else
cout<<-1;
return 0;
}