• codeforces 1015B


    B. Obtaining the String
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given two strings ss and tt. Both strings have length nn and consist of lowercase Latin letters. The characters in the strings are numbered from 11 to nn.

    You can successively perform the following move any number of times (possibly, zero):

    • swap any two adjacent (neighboring) characters of ss (i.e. for any i={1,2,,n1}i={1,2,…,n−1} you can swap sisi and si+1)si+1).

    You can't apply a move to the string tt. The moves are applied to the string ss one after another.

    Your task is to obtain the string tt from the string ss. Find any way to do it with at most 104104 such moves.

    You do not have to minimize the number of moves, just find any sequence of moves of length 104104 or less to transform ss into tt.

    Input

    The first line of the input contains one integer nn (1n501≤n≤50) — the length of strings ss and tt.

    The second line of the input contains the string ss consisting of nn lowercase Latin letters.

    The third line of the input contains the string tt consisting of nn lowercase Latin letters.

    Output

    If it is impossible to obtain the string tt using moves, print "-1".

    Otherwise in the first line print one integer kk — the number of moves to transform ss to tt. Note that kk must be an integer number between 00and 104104 inclusive.

    In the second line print kk integers cjcj (1cj<n1≤cj<n), where cjcj means that on the jj-th move you swap characters scjscj and scj+1scj+1.

    If you do not need to apply any moves, print a single integer 00 in the first line and either leave the second line empty or do not print it at all.

    Examples
    input
    Copy
    6
    abcdef
    abdfec
    output
    Copy
    4
    3 5 4 5
    input
    Copy
    4
    abcd
    accd
    output
    Copy
    -1
    Note

    In the first example the string ss changes as follows: "abcdef" → "abdcef" → "abdcfe" → "abdfce" → "abdfec".

    In the second example there is no way to transform the string ss into the string tt through any allowed moves.

    题意:只能交换s1的相邻元素,为最少需要次可以将s1换成s2

    题解:暴力就行,如果不相等,从当前位置一直找,找到相等的元素然后一个个换过来

    代码如下

    #include <map>
    #include <set>
    #include <cmath>
    #include <ctime>
    #include <stack>
    #include <queue>
    #include <cstdio>
    #include <cctype>
    #include <bitset>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #define fuck(x) cout<<"["<<x<<"]";
    #define FIN freopen("input.txt","r",stdin);
    #define FOUT freopen("output.txt","w+",stdout);
    //#pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    const int maxn = 1e5+5;
    int a[27];
    int b[27];
    int ans[ maxn];
    int main(){
    
        int n;
        char str1[100];
        char str2[100];
        cin>>n>>str1>>str2;
        for(int i=0;i<n;i++){
            a[str1[i]-'a'+1]++;
            b[str2[i]-'a'+1]++;
        }
        int flag=1;
        int m=0;
        for(int i=1;i<=26;i++){
            if(a[i]!=b[i]){
                flag=0;
            }
        }
        if(flag==0){
            puts("-1");
        }else{
            for(int i=0;i<n;i++){
                if(str1[i]==str2[i]){
                    continue;
                }else{
                    for(int j=i; j<n; j++)
                        if(str1[j] == str2[i]){
                            for(int k=j-1; k>=i; k--){
                              ans[m++]=k+1;
                              swap(str1[k], str1[k+1]);
                          }
                          break;
                      }
                  }
              }
              printf("%d
    ",m);
              for(int i=0;i<m;i++){
                printf("%d ",ans[i]);
            }
            puts("");
        }
        return 0;
    }
    View Code
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    stl_hash_set.h
    stl_hash_map.h
    stl_algobase.h
    stl_relops.h
    stl_algo.h
    VC6常用插件
    visual assist(VA)设置快捷键(其它安装的插件设置快捷键也在这里)
    SVN常用命令说明
    Android 调用相册 拍照 实现系统控件缩放 切割图片
    Android 一个3D相册源码
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9416618.html
Copyright © 2020-2023  润新知