Codeforces 254C. Anagram

C. Anagram

time limit per test

1 second

memory limit per test

256 megabytes

input

input.txt

output

output.txt

String x is an anagram of string y, if we can rearrange the letters in string x and get exact string y. For example, strings "DOG" and "GOD" are anagrams, so are strings "BABA" and "AABB", but strings "ABBAC" and "CAABA" are not.

You are given two strings s and t of the same length, consisting of uppercase English letters. You need to get the anagram of string t from string s. You are permitted to perform the replacing operation: every operation is replacing some character from the string s by any other character. Get the anagram of string t in the least number of replacing operations. If you can get multiple anagrams of string t in the least number of operations, get the lexicographically minimal one.

The lexicographic order of strings is the familiar to us "dictionary" order. Formally, the string p of length n is lexicographically smaller than string q of the same length, if p1 = q1, p2 = q2, ..., pk - 1 = qk - 1, pk < qk for some k (1 ≤ k ≤ n). Here characters in the strings are numbered from 1. The characters of the strings are compared in the alphabetic order.

Input

The input consists of two lines. The first line contains string s, the second line contains string t. The strings have the same length (from 1 to 105 characters) and consist of uppercase English letters.

Output

In the first line print z — the minimum number of replacement operations, needed to get an anagram of string t from string s. In the second line print the lexicographically minimum anagram that could be obtained in z operations.

Examples

input

Copy

ABA
CBA

output

Copy

1
ABC

input

Copy

CDBABC
ADCABD

output

Copy

2
ADBADC

Note

The second sample has eight anagrams of string t, that can be obtained from string s by replacing exactly two letters: "ADBADC", "ADDABC", "CDAABD", "CDBAAD", "CDBADA", "CDDABA", "DDAABC", "DDBAAC". These anagrams are listed in the lexicographical order. The lexicographically minimum anagram is "ADBADC".

题意就是给出两个字符串对第一个字符串的若干字符进行修改使之成为第二个字符串的字谜(字谜即字母种数一样相同字母的数量也一样),然后输出需要改的数量和一个字典序最小的答案。

思路操作次数肯定就是第一个字符串多出来的字母不用考虑,然后字典序最小首先对一串进行遍历如果找到一个字符它的出现次数多那么他肯定是要修改的,那么就在这个判断与距离循环一下A~Z,如果说存在一个字符它小于当前位置上的字符并且这个字符在第二个串中的数量多于第一个串中的那就把它换了因为肯定这样会使字典序变小而且还是从A~Z开始也算一种贪心吧,否则那就只剩下>=的情况了这时候就需要把多出来的向后移操作后面的

比如第二个样例他为什么操作第二个B呢就是因为D的字典序比B的大要把不操作的留在前面,换种说法就是假如1串有a个A,2串有b个A(a>b)那么就留a个A在前面修改后面的b-a个。

 

#include<bits/stdc++.h>
using namespace std;
int m1[500],m2[500];
int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    string s1,s2;
    cin>>s1>>s2;
    for(int i=0;i<s1.size();i++)
    {
        m1[s1[i]]++;
        m2[s2[i]]++;
    }
    int ans=0;
    for(int i='A';i<='Z';i++)
    {
        if(m1[i]>m2[i])
            ans+=m1[i]-m2[i];
    }
    for(int i=0;i<s1.size();i++)
    {
        if(m1[s1[i]]>m2[s1[i]]){
            for(int j='A';j<='Z';j++){
                if(j==s1[i]&&m1[j]>0&&m2[j]>0)
                {
                    m1[j]--,m2[j]--;
                    break;
                }
                else if(m2[j]>m1[j])
                {
                    m1[j]++,m1[s1[i]]--;
                    s1[i]=j;
                    break;
                }
            }
        }
    }
    cout<<ans<<endl<<s1<<endl;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值