• CodeForces B. Obtaining the String


    http://codeforces.com/contest/1015/problem/B

    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

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    int N;
    char A[55], B[55];
    vector<int> T;
    map<char, int> mp;
    
    int main() {
        mp.clear();
        scanf("%d", &N);
        scanf("%s%s", A, B);
        for(int i = 0; i < N; i ++) {
            mp[A[i]] ++;
            mp[B[i]] --;
        }
        for(int i = 0; i < N; i ++) {
            if(mp[A[i]]) {
                printf("-1
    ");
                return 0;
            }
        }
        for(int i = 0; i < N; i ++) {
            for(int j = i; j < N; j ++) {
                if(B[i] == A[j]) {
                    for(int k = j; k > i; k --) {
                        swap(A[k], A[k - 1]);
                        T.push_back(k);
                    }
                    break;
                }
            }
        }
    
        printf("%d
    ", T.size());
        for(int i = 0; i < T.size(); i ++) {
            printf("%s", i != 0 ? " " : "");
            printf("%d", T[i]);
        }
        printf("
    ");
        return 0;
    }
    

      

  • 相关阅读:
    Linux ps 命令获取查询结果中的单列信息
    nowcoder(牛客网)普及组模拟赛第一场 解题报告
    Subway Pursuit (二分)(交互题)
    nowcoder(牛客网)OI测试赛2 解题报告
    NOIP提高组题目归类+题解摘要(2008-2017)
    CYJian的水题大赛2 解题报告
    数独问题
    题解 UVA11300 【Spreading the Wealth】
    实验吧web题:
    简单的sql语句
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9845809.html
Copyright © 2020-2023  润新知