CodeForces 254C 字典序
来源:互联网 发布:cassandra 导出数据库 编辑:程序博客网 时间:2024/06/05 07:23
Description
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 1to 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.
Sample Input
ABACBA
1ABC
CDBABCADCABD
2ADBADC
题意:输入两个长度相同的字符串s,t,要求对s进行最少的改动(每次替换一个字母)使s成为t的重组字(字幕出现次序相同,不要求顺序),如果存在多种改动情况,输出最小字典序的那个。
思路:难点在于字典序,这里引入一个松弛概念,如果s中剩余x个D,而需要替换掉x个D,它是不松弛的,遇到D则一定要把它替换掉,而s中剩余x个D,需要替换掉小于x个D,那么它是松弛的,贪心思想,如果替换掉使s字典序变小,那就换,否则就不换。
#include<cstdio>#include<algorithm>#include<string>#include<cstring>#include<sstream>#include<iostream>#include<cmath>#include<queue>#include<map>using namespace std;int main(){freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);map<char,int> cnts,cnt;vector<char> ch;string s,t;// char s[10005],t[10005];cin>>s>>t;for(int i=0;i<s.size();i++){cnts[s[i]]++;cnt[s[i]]++;}for(int i=0;i<t.size();i++){cnt[t[i]]--;}int num=0;for(int i=0;i<s.size();i++){cnts[s[i]]--;if(cnt[s[i]]>0){//需替换 if(cnts[s[i]]+1<=cnt[s[i]]){ //不松弛,一定替换 // printf("debug\n");for(char j='A';j<='Z';j++){if(cnt[j]<0){cnt[s[i]]--;s[i]=j;cnt[j]++;num++;break;}}}else{ //松弛,只替换使字典序变小的 for(int j='A';j<='Z';j++){if(j>s[i]) break;if(cnt[j]<0){cnt[s[i]]--;s[i]=j;cnt[j]++;num++;break;;}}}}}cout<<num<<"\n"<<s<<endl;return 0;}
- CodeForces 254C 字典序
- [CodeForces 510C]Fox And Names[字典序][拓扑排序]
- codeforces 632C(字符串按字典序排列)
- codeforces C. Watto and Mechanism 字典树
- Codeforces 197C 找最大子串(字典序 可以不连续)
- Codeforces Round #315 (Div. 1) C. New Language 字典序 2-sat 求解
- Codeforces 600C Make Palindrome 【贪心 找字典序最小回文串】
- Codeforces 514C Watto and Mechanism (字典树+dfs)
- Codeforces 633C Spy Syndrome 2 【字典树 + DFS】
- Codeforces 633C Spy Syndrome 2 字典树
- Codeforces Round #402 (Div. 1) C. Peterson Polyglot(字典树合并/可持久化字典树)
- 【Educational Codeforces Round 2C】【贪心】最少修改数下得到字典序尽可能小回文串
- 字典(C#)
- c# 字典
- Codeforces 557E dp+字典树+字典序第k大
- C. Watto and Mechanism 字典树 Codeforces Round #291 (Div. 2)
- Codeforces Round #291 (Div. 2)C.Watto and Mechanism——字典树+dfs
- Codeforces Round #291 (Div. 2) C. Watto and Mechanism Trie字典树+dfs
- linux用户相关及/etc/passed,/etc/group,/etc/shadow
- 做一件IT相关的事必须的要素
- node版本n模块升级的路径问题
- 延迟求值主动求值
- linux关于readlink函数获取运行路径的小程序
- CodeForces 254C 字典序
- C++中继承和调用父类的构造函数方法,外加MFC常见dialog继承
- 第二章 基本简单小算法
- python中sys.exit() os._exit() exit() quit()的简单使用
- intent详解(一)
- 一张图读懂嵌入式开发
- LLT测试(1)读写BIN文件
- oracle的 归档日志 archivelog
- 树(Tree)