首页 > 其他 > 详细

[CF254C]Anagram(2019-11-15考试)

时间:2019-11-15 11:20:59      阅读:88      评论:0      收藏:0      [点我收藏+]

题目大意

给你两个长度相同的字符串\(A,B\),要求改变字符串\(A\)中最少的字符,使得字符串\(A\)在排序后和字符串\(B\)相同。输出改变后的字符串\(A\),若多解,输出字典序最小的。\(|A|=|B|\leqslant10^5\)

题解

统计\(A\)\(B\)中每种字符出现次数,肯定是把出现\(A\)中次数过多字符的改成不够的字符。首先,肯定按字典序从小到大改字符,然后根据字符大小,确定是把多的字符中后面部分改成一种字符还是把前面的一部分改成一种字符即可

卡点

C++ Code:

#include <cstdio>
#include <iostream>
#include <algorithm>

int n, cntA[26], cntB[26], pos;
std::string A, B, T;
int main() {
    std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
    std::cin >> A >> B, n = A.length();
    for (int i = 0; i < n; ++i) ++cntA[A[i] - 'A'];
    for (int i = 0; i < n; ++i) ++cntB[B[i] - 'A'];
    for (int i = 0; i < 26; ++i) {
        for (int j = cntA[i]; j < cntB[i]; ++j)
            T += static_cast<char> ('A' + i);
    }
    std::cout << T.length() << '\n';
    for (int i = 0, c; i < n; ++i) {
        c = A[i] - 'A';
        if (cntA[c] > cntB[c]) {
            if (T[pos] < A[i] || !cntB[c])
                std::cout << T[pos], --cntA[c], --cntB[T[pos++] - 'A'];
            else std::cout << A[i], --cntA[c], --cntB[c];
        } else std::cout << A[i], --cntA[c], --cntB[c];
    }
    std::cout.put('\n');
    return 0;
}

[CF254C]Anagram(2019-11-15考试)

原文:https://www.cnblogs.com/Memory-of-winter/p/11864887.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!