Codeforces 955B - Not simply beatiful strings(模拟)

Problem Description

Let's call a string adorable if its letters can be realigned in such a way that they form two consequent groups of equal symbols (note that different groups must contain different symbols). For example, ababa is adorable (you can transform it to aaabb, where the first three letters form a group of a-s and others — a group of b-s), but cccc is not since in each possible consequent partition letters in these two groups coincide.

You're given a string s. Check whether it can be split into two non-empty subsequences such that the strings formed by these subsequences are adorable. Here a subsequence is an arbitrary set of indexes of the string.

Input   

The only line contains s (1 ≤ |s| ≤ 105) consisting of lowercase latin letters.

Output 

Print «Yes» if the string can be split according to the criteria above or «No» otherwise.

Each letter can be printed in arbitrary case.

Examples 

Input
ababa
Output
Yes
Input
zzcxx
Output
Yes
Input
yeee
Output
No

Note

In sample case two zzcxx can be split into subsequences zc and zxx each of which is adorable.

There's no suitable partition in sample case three.

题意:

让我们把一个串分成两部分,使得这两部分子串的每个子串都仅由两种字母组成(这里的子串可以随意选字母组不一定按照原串的顺序)

思路:

我们可以先统计一下整个串的字母种类数,如果只有1种或大于四种一定是不满足条件的,所以答案就在234中,字母种类为2时,必须保证每种字母个数大于等于2,而字母种类为3时,只要有一个种类的字母个数大于等于2即可,即作为那个公共的字母,另外两种字母一个串一种,当四个的时候一定符合,不管字母多少个。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5+10;
int main(){
    char s[maxn];
    scanf("%s",s);
    int vis[27];
    memset(vis,0,sizeof(vis));
    int cnt = 0;
    for(int i = 0; s[i]; i++){
        vis[s[i]-'a']++;
    }
    for(int i = 0; i < 26; i++){
        if(vis[i]) cnt++;
    }
    if(cnt >= 5 || cnt < 2) printf("No\n");
    else{
        if(cnt == 2){
            int flag = 1;
            for(int i = 0; i < 26; i++){
                if(vis[i] == 1){
                    flag = 0;
                    break;
                }
            }
            if(flag) printf("Yes\n");
            else printf("No\n");
        }
        else if(cnt == 3){
            int flag = 0;
            for(int i = 0; i < 26; i++){
                if(vis[i] > 1){
                    flag = 1;
                    break;
                }
            }
            if(flag) printf("Yes\n");
            else printf("No\n");
        }
        else{
            printf("Yes\n");
        }
    }
 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wcxyky/article/details/90176478