Codeforces Round 471-2B题解报告

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

一、分析

(一)什么是adorable串?

(1) ”ababa”,可以分成两部分”aaa”和”bb“。每部分的字符都完全一样,并且两部分的组成字符不一样,分别是’a’和’b’。这样,”ababa”就是一个adorable串。
(2)“cccc”,要么分成”c”和”ccc”,要么分成”cc”和”cc”。无论哪种分法,两部分的组成字符都是”c”,因此”cccc”不是一个adorable串。
(3)”zx”,可以分成”z”和”x”,每部分的字符都一样(每部分只有一个字符肯定一样),两部分的组成字符不一样:一部分由’z’组成,另一部分由’x’组成。所以”zx”是一个adorable串
(4)“zxx”,可以分成”z”和”xx”,每部分的字符都一样,两部分的组成字符不一样:一部分由’z’组成,另一部分由’x’组成,所以”zxx”是一个adorable串。

(二)题意

如果能把一个字符串分成两份子串,每份都是adorable串(即每份都可以被分成只有相同字母的两块,并且这两块的字母不相同),输出”Yes”,否则输出”No”。
有下列几种情况:
(1)字符串长度小于4,这种情况不够分,输出”No”
(2)如果包含4种以上的字符,肯定无法做到两个子串都是adorable,输出”No”。
比如”abcdeee”,这里有五种字符。两个子串四部分,有一部分会由两种字符组成,不符合adorable的定义。
(3)如果包含4种不同的字符,则结果一定是”Yes”。比如“aabcdd”,子串”aab”分成”aa”和”b”,子串”cdd”分成”c”和”dd”,则”aab”和”cdd”都是adorable串,符合题意。
(4)如果包含3种不同的字符并且字符串长度不小于4,输入”Yes”。比如”abcc“的两个子串”ac”和”bc”都是adorable串。
(5)如果包含2种不同的字符并且字符串长度不小于4,要分具体的情况。
① 假如有一种字符只出现了一次,则另一种字符必然出现的次数不少于3次。此时输出”No”。比如”abbb”,只能分成”ab”和”bb”,”ab”是adorable串,”bb”不是adorable串故不合题意。
② 假如两种字符出现的次数都不少于两次,则输出”Yes”。比如”aabbb”,可分为”ab”和”abb”,都是adorable串,故符合题意。
(6)如果只包含1种字符,即所有的字符都相同,输出”No”

二、代码

#include <iostream>
using namespace std;

int main()
{
    string s;
    int m[256];
    // 出现次数不少于两次的字符个数
    int charMoreThanOnce = 0;
    // 总字符个数
    int charCnt = 0;

    for (int i = 0; i < 256; i++)
    {
        m[i] = 0;
    }

    cin >> s;
    for (int i = 0; i < (int)s.length(); i++)
    {
        m[(int)s[i]]++;
    }

    for (int i = (int)'a'; i <= (int)'z'; i++)
    {
        if(m[i] > 0)
        {
            charCnt++;
        }


        if(m[i] > 1)
        {
            charMoreThanOnce++;
        }
    }

    if (charCnt <= 4 && charMoreThanOnce + charCnt >= 4)
    {
        cout << "Yes";
    }
    else
    {
        cout << "No";
    }

    return 0;
}


Codeforces & TopCoder QQ交流群:648202993
更多内容请关注微信公众号
feicuisenlin_12x12.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值