cf div3 B Obtaining the String

先贴上我的wa代码,至今仍未找到bughttps://paste.ubuntu.com/p/K3FWdQmQ9m/

  我的思路就是冒泡排序,最多冒n-1次。

  后来看来大佬的做法,也是冒泡排序,不过,是给s串里每一个字符都赋予了一个值,这个值是依照t串赋予的,比如说我们遍历到了t串的i位置的字符,那么t串里i位置的字符对应的值为i,然后s串里和t[i]相等且未被赋予值的字符赋予和t[i]一样的值即i。那么最后

得到了一个t串对应的序列,从1到n是升序的,还得到了一个s串对应的序列,是无序的,对其冒泡排序即可

#include<bits/stdc++.h>
using namespace std;
char st[55],et[55];
int cnt1[26],cnt2[26];
int stn[55],etn[55],n;
int main()
{
    queue<int>ans;
    while(!ans.empty()) ans.pop();
    cin>>n>>st+1>>et+1;
    int flag=1;
    for(int i=1;i<=n;i++)
    {
        if(flag&&st[i]!=et[i]) flag=0;
        cnt1[st[i]-'a']++;
        cnt2[et[i]-'a']++;
    }
    if(flag)
    {
        cout<<0<<endl;
        return 0;
    }
    for(int i=0;i<=25;i++)
    {
        if(cnt1[i]!=cnt2[i])
        {
            cout<<-1<<endl;
            return 0;
        }
    }
    for(int i=1;i<=n;i++)
    {
        etn[i]=i;
        for(int j=1;j<=n;j++)
        {
            if(et[i]==st[j]&&!stn[j])
            {
                stn[j]=i;
                break;
            }
        }
    }
    for(int i=0;i<n-1;i++)
        for(int j=1;j<n-i;j++)
        {
            if(stn[j]>stn[j+1])
            {
                swap(stn[j],stn[j+1]);
                ans.push(j);
            }

        }
    cout<<ans.size()<<endl;
    while(!ans.empty())
    {
        cout<<ans.front()<<" ";
        ans.pop();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40642465/article/details/81349407
今日推荐