[Codeforces] 1015B. Obtaining the String

題目URL: http://codeforces.com/contest/1015/problem/B
另類的bubble sort(?


#include<bits/stdc++.h>
using namespace std;
int str[10005],num,flag=0;
string a,b,c,d;
main()
{
    cin >> num;
    cin >> a >> b;
    c=a; d=b;
    sort(c.begin(),c.end());
    sort(d.begin(),d.end());
    if(c!=d)
        cout << -1 << endl;
    else
    {
        for(int i=0;i<num;i++)
            if(a[i]!=b[i])
                for(int j=i;j<num;j++)
                    if(a[j]==b[i])
                    {
                        for(int k=j;k>i;k--)
                        {
                            swap(a[k],a[k-1]);
                            str[flag++]=k;
                        }
                        break;
                    }
        cout << flag << endl;
        for(int i=0;i<flag;i++)
            cout << str[i] << " ";
        cout << endl;
    }
    return 0;
}

留言

這個網誌中的熱門文章

[ZJ] b229: TOI 2009 第一題:路徑問題

交大資工(APCS組)(面試&心得)

滿是挫傷的ION CAMP