码迷,mamicode.com
首页 > 其他好文 > 详细

【codeforces 1109B】Sasha and One More Name

时间:2019-04-06 16:43:56      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:for   else   组成   pos   ace   int   分割   bst   mes   

【链接】 我是链接,点我呀:)
【题意】


题意

【题解】


如果这个回文串的左半部分,字母全是一样的。
那么显然不可能再分出来了,因为不管怎么分怎么排列,最后肯定都只能和原串一样。
所以无解
其他情况下,都有解。
可以保证答案是<=2的
比如
abxxxxxxxba
我们可以把ba放在前面来,ab放在后面去
baxxxxxxxab
也是符合题意的。
即把前i个字符组成的不是回文(因为至少有两种字母,所以肯定能找到)的前缀以及它对应长度的后缀调换一下顺序.
所以肯定有答案为2的情况。
接下来分析答案为1的情况。
这种时候就枚举一个分割点。
然后交换前缀和后缀顺序即可

【代码】

#include <bits/stdc++.h>
using namespace std;

string s;

bool check(string s){
    for (int i = 1;i < (int)s.size()/2;i++){
        if (s[i]!=s[0]) return true;
    }
    return false;
}

bool ispar(string s){
    for (int i = 0;i < (int)s.size()/2;i++){
        if (s[i]!=s[(int)s.size()-i-1]){
            return false;
        }
    }
    return true;
}

bool ok(string s){
    string t = s;
    for (int i = 0;i < (int)s.size();i++){
        t = t.substr(1) + t[0];
        if (t!=s && ispar(t)) return true;
    }
    return false;
}

int main(){
    cin >> s;
    if (!check(s)){
        cout<<"Impossible"<<endl;
    }else if (ok(s)){
        cout<<1<<endl;
    }else{
        cout<<2<<endl;
    }
    return 0;
}

【codeforces 1109B】Sasha and One More Name

标签:for   else   组成   pos   ace   int   分割   bst   mes   

原文地址:https://www.cnblogs.com/AWCXV/p/10661932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!