CodeForces - 936C ——Lock Puzzle

題目鏈接:https://vjudge.net/contest/336724#problem/C

題意:只能進行一種操作,選定一個x,最後x個字符翻轉,然後把翻轉後的字符放到最前面,求是否能在6100次內把s串通過上述操作變成t串

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n;
char s[6110][2100],t[2100];
int A[456],ans[6410],tot,cnt;
int judge()
{
    for(int i=0;i<n;i++)
        A[s[0][i]]++,A[t[i]]--;
    for(int i='a';i<='z';i++)
        if(A[i]!=0) return 0;
    return 1;
}
void rev(int x)
{
    int r=0;
    for(int i=n-1;i>=n-x;i--)
    {
        s[tot+1][r++]=s[tot][i];
    }
    for(int i=0;i<n-x;i++)
    {
        s[tot+1][r++]=s[tot][i];
    }
    ans[++tot]=x;
}
void head()
{
    for(int i=0;i<n;i++)
    {
        if(s[tot][i]==t[cnt])
        {
            rev(n-i-1);
            rev(1);
            ++cnt;
            return ;
        }
    }
}
void zheng()
{
    rev(n-cnt);
    for(int i=0;i<n;i++)
    {
        if(s[tot][i]==t[cnt])
        {
//            printf("-->%d %c %c\n",i,t[cnt],s[tot][i]);
            rev(n-i-1);
            rev(1);
            ++cnt;
            return ;
        }
    }
}
void fan()
{
    for(int i=cnt;i<n;i++)
    {
        if(s[tot][i]==t[cnt])
        {
            rev(n-i);
            rev(i-cnt);
            rev(++cnt);
            return ;
        }
    }
}
int main()
{
    scanf("%d%s%s",&n,s[0],t);
    if(judge()==0)
    {
        printf("-1\n");
        return 0;
    }
    head();
    int f=0;
    while(cnt<n)
    {
        if(f%2==0) zheng();
        else fan();
        f++;
    }
    if(f%2) rev(n);
//    for(int i=0;i<=tot;i++)
//    {
//        printf("%s\n",s[tot]);
//    }
    printf("%d\n",tot);
    for(int i=1;i<=tot;i++) printf("%d%s",ans[i],i==tot?"\n":" ");
}
/*
10
3526140897
1234567890
*/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章