• Codeforces Round #467 (Div. 1). C


    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <vector>
    const int maxLength = 2005;
    
    char s[maxLength], t[maxLength];
    int letterOfS[30];
    int letterOfT[30];
    std::vector<int> result;
    void shift(char* seqence, int location) {
        std::string tt = seqence;
        tt = tt.substr(location) + tt.substr(0, location);
        std::reverse(tt.begin(), tt.begin() + tt.length() - location);
        for (int i = 0, length = tt.length(); i < length; ++i) {
            seqence[i] = tt[i];
        }
        //   seqence[tt.length()] = 0;
    
        result.push_back(tt.length() - location);
        //   printf("%s
    ", seqence);
    }
    void solveChange(int length) {
        int okLength = 0;
        for (int i = 0; i < length; ++i) {
            char targetLetter = t[i];
            int LocationOftargetInS;
            for (int j = 0; j < length - i; ++j) {
                if (s[j] == targetLetter) {
                    LocationOftargetInS = j;
                    break;
                }
            }
            //   printf("%d
    ", LocationOftargetInS);
            shift(s, LocationOftargetInS + 1);
            shift(s, length - 1);
            shift(s, 0);
            //    printf("
    ");
        }
    }
    
    int main() {
        int n;
        while (~scanf("%d", &n)) {
            result.clear();
            memset(letterOfS, 0, sizeof(letterOfS));
            memset(letterOfT, 0, sizeof(letterOfT));
    
            scanf("%s %s", s, t);
            for (int i = 0; i < n; ++i) {
                letterOfS[s[i] - 'a']++;
            }
            for (int i = 0; i < n; ++i) {
                letterOfT[t[i] - 'a']++;
            }
            bool canChange = true;
            for (int i = 0; i < 26 && canChange; ++i) {
                if (letterOfS[i] != letterOfT[i]) {
                    canChange = false;
                }
            }
    
            if (canChange) {
                solveChange(n);
                printf("%d
    ", (int)result.size());
                for (int i = 0; i < result.size(); ++i) {
                    printf("%d ", result[i]);
                }
                printf("
    ");
            } else
                printf("-1
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    hdu1418 欧拉公式
    hdu1215七夕节 筛选法求公因子和
    hdu1215 The area
    hdu1005Number Sequence
    hdu1021 数学题 并不是说难,而是数学题那种简单朴素的思想get不到
    Mongo第三个参数的用法
    js 显示刚刚上传的图片 (onchange事件)
    在linux中安装memcache服务器
    jQuery 倒计时
    PHP获取文章发布时间
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8620541.html
Copyright © 2020-2023  润新知