Codeforces Round #324 (Div. 2) C. Marina and Vasya 字符串处理
2015-10-07 09:26
417 查看
C. Marina and Vasya
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.
More formally, you are given two strings s1, s2 of length n and number t. Let’s denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print - 1.
Input
The first line contains two integers n and t (1 ≤ n ≤ 105, 0 ≤ t ≤ n).
The second line contains string s1 of length n, consisting of lowercase English letters.
The third line contain string s2 of length n, consisting of lowercase English letters.
Output
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn’t exist, print -1.
Sample test(s)
input
3 2
abc
xyc
output
ayd
input
1 0
c
b
output
-1
题意, 要求与两个字符串的不同字符都为t的第三个字符串。
先求出两个字符串的相同的数num.如果,n - t <= num,则只需要把这num个相同的字符选其中的一部分就可以了。否则,尽可能的一半与1字符串相同,别一半与2字符串相同。若仍然不够,则无解。
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.
More formally, you are given two strings s1, s2 of length n and number t. Let’s denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print - 1.
Input
The first line contains two integers n and t (1 ≤ n ≤ 105, 0 ≤ t ≤ n).
The second line contains string s1 of length n, consisting of lowercase English letters.
The third line contain string s2 of length n, consisting of lowercase English letters.
Output
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn’t exist, print -1.
Sample test(s)
input
3 2
abc
xyc
output
ayd
input
1 0
c
b
output
-1
题意, 要求与两个字符串的不同字符都为t的第三个字符串。
先求出两个字符串的相同的数num.如果,n - t <= num,则只需要把这num个相同的字符选其中的一部分就可以了。否则,尽可能的一半与1字符串相同,别一半与2字符串相同。若仍然不够,则无解。
#define N 300005 #define M 100005 #define maxn 205 #define MOD 1000000000000000007 int n,t,num; char s1 ,s2 ,ans ; int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while(S2(n,t)!=EOF) { SS(s1); SS(s2); num = 0; t = n - t; FI(n) ans[i] = s1[i]; ans = '\0'; FI(n){ if(s1[i] == s2[i]) num++; } if(num >= t){ FI(n){ if(t > 0){ if(s1[i] == s2[i]){ ans[i] = s1[i]; t--; } else { FJ(26){ char c = 'a' + j; if(s1[i] != c && s2[i] != c){ ans[i] = c; break; } } } } else { FJ(26){ char c = 'a' + j; if(s1[i] != c && s2[i] != c){ ans[i] = c; break; } } } } printf("%s\n",ans); } else if((n - num) / 2 + num >= t){ //printf("%d %d %d\n",n,num,t); int n1 = t - num; int n2 = n1; int first = 1; FI(n){ if(s1[i] == s2[i]){ ans[i] = s1[i]; } else { if(first == 1){ ans[i] = s1[i]; n2--; if(n2 == 0){ n2 = n1; first++; } } else if(first == 2){ ans[i] = s2[i]; n2--; if(n2 == 0){ n2 = n1; first++; } } else { FJ(26){ char c = 'a' + j; if(s1[i] != c && s2[i] != c){ ans[i] = c; break; } } } } } printf("%s\n",ans); } else { printf("-1\n"); } } //fclose(stdin); //fclose(stdout); return 0; }
相关文章推荐
- 数据库链接字符串查询网站
- Flex字符串比较 还有Flex字符串操作
- Ruby中创建字符串的一些技巧小结
- ASP下经常用的字符串等函数参考资料
- 将字符串小写转大写并延时输出的批处理代码
- 将字符串转换成System.Drawing.Color类型的方法
- Lua源码中字符串类型的实现
- Lua性能优化技巧(四):关于字符串
- 字符串聚合函数(去除重复值)
- Ruby中的字符串编写示例
- 总结的5个C#字符串操作方法分享
- sqlserver中求字符串中汉字的个数的sql语句
- sql server字符串非空判断实现方法
- VBS的字符串及日期操作相关函数
- C#实现将千分位字符串转换成数字的方法
- jquery 删除字符串最后一个字符的方法解析
- PowerShell实现在字符串中查找大写字母
- PowerShell中使用Out-String命令把对象转换成字符串输出的例子
- PowerShell中字符串使用单引号和双引号的区别
- Powershell小技巧之获取字符串的行数