zoukankan      html  css  js  c++  java
  • 【codeforces 550A】Two Substrings

    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.

    Examples
    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”.

    【题目链接】:http://codeforces.com/contest/550/problem/A

    【题解】

    把所有的ABABABA…以及BABA…这两种形式的子串“整个”提出来;
    把它们的长度分别放在两个vector,ab以及ba中。
    如果最后ab和ba两个vecto的size都大于0,则yes
    如果ab或ba中最大的数字(也即ABAB..的长度或BABAB…的长度)大于等于5
    则yes(因为ABABA中可以找到AB和BA,BABAB中也可以找到BAAB,且这是所需的最短的串);
    又或者,ab或ba中有一个大小>=3,还有一个大小>=2,则也可行
    比如ABTABA
    ab最后有两个元素分别为2和3;
    可以看到能够找到AB和BA.
    BATBAB同理
    也能找到BA和AB;
    其他情况NO;

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    //const int MAXN = x;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    const int MAXN = 1e5+100;
    
    string s;
    vector <int> ab,ba;
    int ab3,ba3,ab5,ba5;
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        cin >> s;
        s = ' '+s;
        int len = s.size()-1;
        rep1(i,1,len)
            {
                if (s[i]=='A' && i+1<=len && s[i+1]=='B')
                {
                    int r = i;
                    while ((r+1<=len) && (s[r+1] == 'A' || s[r+1]=='B') && s[r+1]!=s[r])
                        r++;
                    int le = r-i+1;
                    if (le>=3)
                    {
                        ab3++;
                        if (le>=5)
                            ab5++;
                    }
                    ab.pb(le);
                    i = r;
                }
                else
                    if (s[i]=='B' && i+1<=len && s[i+1]=='A')
                    {
                        int r = i;
                        while ((r+1<=len) && (s[r+1] == 'A' || s[r+1]=='B') && s[r+1]!=s[r])
                            r++;
                        int le = r-i+1;
                        ba.pb(le);
                        if (le>=3)
                        {
                            ba3++;
                            if (le>=5)
                                ba5++;
                        }
                        i = r;
                    }
            }
        int lenab = ab.size(),lenba = ba.size();
        if (lenab>0 && lenba >0)
        {
            puts("YES");
            return 0;
        }
        if (ab5||ba5)
        {
            puts("YES");
            return 0;
        }
        if (lenab >1 && ab3)
        {
            puts("YES");
            return 0;
        }
        if (lenba >1 && ba3)
        {
            puts("YES");
            return 0;
        }
        puts("NO");
        return 0;
    }
    
  • 相关阅读:
    国内三大云数据库测试对比
    Nginx源码安装及调优配置
    如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理
    开学了!这些Linux认证你要知道
    快速入门SaltStack
    WPF模板
    C#基础知识回顾-- 属性与字段
    程序员接私活经验谈[转]
    强烈推荐:240多个jQuery插件
    [Java]读取文件方法大全
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626789.html
Copyright © 2011-2022 走看看