I - Playing With Strings(字符能否够成对称)

I - Playing With Strings

 Gym - 101020I 

注意分一下奇偶:

#include<bits/stdc++.h>
using namespace std;
int mmp[30];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int sum=0;
        memset(mmp,0,sizeof(mmp));
        string str;
        char ans[1005],cnt;
        int m=0;
        cin>>str;
        int len=str.size();
        for(int i=0; i<len; i++)
            mmp[str[i]-'a'+1]++;
        if(len%2==0)
        {
            for(int i=1; i<27; i++)
                if(mmp[i]%2==1)
                    sum++;
            if(sum>0)
                cout<<"impossible";
            else
            {
                for(int i=1; i<27; i++)
                    if(mmp[i]>0)
                    {
                        mmp[i]=mmp[i]/2;
                        while(mmp[i]--)
                        {
                            char temp=i-1+'a';
                            ans[m++]=temp;
                            cout<<temp;
                        }
                    }
            }
            for(int i=m-1; i>=0; i--)
                cout<<ans[i];
        }
        else
        {
            for(int i=1; i<27; i++)
                if(mmp[i]%2==1)
                    sum++;
            if(sum!=1)
                cout<<"impossible";
            else
            {
                for(int i=1; i<27; i++)
                    if(mmp[i]>0)
                    {
                        if(mmp[i]%2==1)
                        {
                            cnt=i-1+'a';
                            mmp[i]--;
                        }
                        if(mmp[i]%2==0)
                        {
                            mmp[i]=mmp[i]/2;
                            while(mmp[i]--)
                            {

                                char temp=i-1+'a';
                                ans[m++]=temp;
                                cout<<temp;
                            }
                        }
                    }
                cout<<cnt;
                for(int i=m-1; i>=0; i--)
                    cout<<ans[i];
            }
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/BePosit/article/details/81359093
今日推荐