您的位置:首页 > 运维架构

AC日记——字符串最大跨距 openjudge 1.7 26

2016-11-27 09:10 399 查看

26:字符串最大跨距

总时间限制: 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<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

int len,now=1,len_3[4],cur=0,ans_l=-1,ans_r=-1;

char word[400],word_1[4][400];

int main()
{
gets(word);
len=strlen(word);
for(int i=0;i<len;i++)
{
if(word[i]==',')
{
len_3[now]=cur;
now++;
cur=0;
continue;
}
word_1[now][cur++]=word[i];
}
len_3[now]=cur;
for(int i=0;i<len_3[1];i++)
{
if(word_1[1][i]==word_1[2][0])
{
bool if_ok=true;
for(int j=i;j<=i+len_3[2]-1;j++)
{
if(word_1[1][j]==word_1[2][j-i]) continue;
if_ok=false;
break;
}
if(if_ok)
{
ans_r=i+len_3[2]-1;
break;
}
}
}
for(int i=len_3[1]-1;i>=len_3[3]-1;i--)
{
if(word_1[1][i]==word_1[3][len_3[3]-1])
{
bool if_ok=true;
for(int j=i;j>=i-len_3[3]+1;j--)
{
if(word_1[1][j]==word_1[3][len_3[3]+j-i-1]) continue;
if_ok=false;
break;
}
if(if_ok)
{
ans_l=i-len_3[3]+1;
break;
}
}
}
if(ans_l==-1||ans_r==-1||ans_l<=ans_r)
{
printf("-1\n");
}
else
{
printf("%d\n",ans_l-ans_r-1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: