天天看点

codeforces round div2 B. Ugly Pairs

题意: 给你一个串 问你是否能够重排列使相邻的两个数没有出现在字母表中的位置

比如给你 abe 重排列后为 aeb ab在字母表相邻 就不能让其出现

思路: 将输入的字符串分奇偶考虑 奇数的一串 偶数的一串 奇数串和偶数串一定满足条件的 唯一需要考虑的就是两个串相邻的地方满不满足条件 数据很小 就从奇数串的位置出发去偶数串找满足两者相减的绝对值大于等于2的两个数 如果有这两个数 那么就是存在能够使他们相连接的情况 记录下数字的位置和大小 然后进行输出就可以了

代码如下 需要特别注意的是 : 需要特判一下两串是否为空

#include<bits/stdc++.h>

using namespace std;
#define se second
#define fi first
#define pb push_back
typedef long long ll;
typedef pair<int,int> p;
const int maxn = 2e5+10;

vector<int> odd,even;

int main()
{
    int t;
    cin>>t;
    while(t--){
        string s;
        odd.clear();even.clear();
        cin>>s;
        for(int i = 0; i < s.size(); i++){
            if(((int)s[i] - 96)%2==0)
                odd.pb(((int)s[i] - 96));
            else
                even.pb(((int)s[i] - 96));
        }
        int l,posl = 100000,posr = 100000,r,flag = 0;
        for(int i = 0; i < odd.size(); i++)
        {
            for(int j = 0; j < even.size(); j++)
            {
                if(abs(odd[i] - even[j]) >= 2){
                    l = odd[i],posl = i;r = even[j];posr = j;
                    flag = 1;
                    break;
                }
            }
        }
        if(flag||odd.size()==0||even.size()==0){
            for(int i = 0; i < odd.size(); i++){
                if(i==posl) continue;
                else cout<<(char)(odd[i]+96);
            }
            if(odd.size()!=0&&even.size()!=0){
                cout<<(char)(l+96);
                cout<<(char)(r+96);
            }
            for(int j = 0; j < even.size(); j++){
                if(j==posr) continue;
                else cout<<(char)(even[j]+96);
            }
        }
        else
            cout<<"No answer";
        cout<<endl;
    }
    return 0;
}