Codeforces Round #306 (Div. 2) A. Two Substrings

来源:互联网 发布:网络言论自由权的内涵 编辑:程序博客网 时间:2024/05/13 03:28

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings “AB” and “BA” (the substrings can go in any order).

Input
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.

Output
Print “YES” (without the quotes), if string s contains two non-overlapping substrings “AB” and “BA”, and “NO” otherwise.

Sample test(s)
input
ABA
output
NO
input
BACFAB
output
YES
input
AXBYBXA
output
NO
Note
In the first sample test, despite the fact that there are substrings “AB” and “BA”, their occurrences overlap, so the answer is “NO”.

In the second sample test there are the following occurrences of the substrings: BACFAB.

In the third sample test there is no substring “AB” nor substring “BA”.

这道题的意思很简单,就是在输入的字符串中找有没有AB或BA,但是不能是重叠在一起,第一个例子就是重叠在一起的,所以输出NO。

这道题分两种可能,一种是前面找到了ABA或BAB,这个时候只要在后面找到AB或者BA就可以输出YES,如果没有,则要找到AB和BA,如果两个都可以找到,就可以输出YES,否则输出NO。

#include<iostream>#include<string.h>using namespace std;char a[100010];int b[100010];int main(){    while(cin>>a)    {        int len=strlen(a);        if(len<4)  cout<<"NO"<<endl;        else        {            int flag=0;            int count=0;            int ans=0;            int nut=0;            int k=0;            for(int i=0;i<len;i++)            {                if(k==0)                {                    if((a[i]=='A'&&a[i+1]=='B'&&a[i+2]=='A')||(a[i]=='B'&&a[i+1]=='A'&&a[i+2]=='B'))                    {                        flag=1;                        nut=i+2;                         k=1;                    }                }                if(flag==1&&i>nut)                {                    if((a[i]=='A'&&a[i+1]=='B')||(a[i]=='B'&&a[i+1]=='A'))  ans=1;                }            }            if(ans==1)   cout<<"YES"<<endl;            else            {                flag=0;                count=0;                for(int i=0;i<len;i++)                {                    if(a[i]=='A'&&a[i+1]=='B'&&b[i]==0&&b[i+1]==0)                    {                        flag=1;                        b[i]=1;                        b[i+1]=1;                    }                    if(a[i]=='B'&&a[i+1]=='A'&&b[i]==0&&b[i+1]==0)                    {                        count=1;                        b[i]=1;                        b[i+1]=1;                    }                    if(flag==1&&count==1)  break;                }                if(flag==1&&count==1)  cout<<"YES"<<endl;                else  cout<<"NO"<<endl;            }        }        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));    }    return 0;}

然后我同学的代码更加简洁,思路一样。

#include<cstdio>#include<cstring>using namespace std;char s[100010];int main(){    int f=0,g=0,sp=0,flag=0;    scanf("%s",s);    int l=strlen(s);    for(int i=0;i<l-1;++i)    {        if(!sp)        {            if( (s[i]=='A' && s[i+1]=='B' && s[i+2]=='A') || (s[i]=='B' && s[i+1]=='A' && s[i+2]=='B') )            {                sp=1; i+=2;continue;            }               }        if(!f && s[i]=='A' && s[i+1]=='B')         {            f=1; i++;           }               else if(!g && s[i]=='B' && s[i+1]=='A')         {            g=1; i++;        }         if((f && g)|| ( (f || g) && sp ) )         {            flag=1; break;        }    }    if(flag) printf("YES\n");    else printf("NO\n");    return 0;}
0 0
原创粉丝点击