codeforces 324# C. Marina and Vasya (贪心)
2015-10-10 18:15
471 查看
题目:http://codeforces.com/contest/584/problem/C
题意:给定两个字符串s1和s2,求字符串s3使得s3与s1对应位置字符不同的个数为t,并且s3与s2对应位置字符不同的个数也为t。
C. Marina and Vasya
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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
output
input
output
分析:如果构造不出s3,就输出-1。由于找一个与另外两个字符的字符很好找,那么构造不出s3的情况是,s3和s1,s3和s2对应位置相同字符的个数不足。那么最大限度地利用一个位置的贡献就好了,怎么最大限度利用一个位置?当s1[i]==s2[i]的时候,把s3[i]赋成s1[i]。剩下的位置s1和s2交替贡献字符就行了。最后看满不满足条件。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e6;
char s1[maxn],s2[maxn],ans[maxn];
int main()
{
int n,t,i,j,lack;
cin>>n>>t;
cin>>s1>>s2;
lack=n-t;
int cnt1=0,cnt2=0;
memset(ans,'\0',sizeof(ans));
for(i=0;i<n;i++)
{
if(cnt1==lack)
break;
if(s1[i]==s2[i])
{
ans[i]=s1[i];
s1[i]=s2[i]='#';
cnt1++;
cnt2++;
}
}
// printf("%s %s %s\n",ans,s1,s2);
int fg=1;
for(i=0;i<n;i++) if(s1[i]!='#')
{
if(cnt1==lack && cnt2==lack)
break;
if(cnt1==cnt2)
{
ans[i]=s1[i];
cnt1++;
}
else
{
ans[i]=s2[i];
cnt2++;
}
s1[i]=s2[i]='#';
}
// printf("%s %s %s",ans,s1,s2);
for(i=0;i<n;i++) if(s1[i]!='#')
{
for(char ch='a';ch<='z';ch++)
{
if(ch!=s1[i] && ch!=s2[i])
{
ans[i]=ch;
break;
}
}
}
if(cnt1!=lack || cnt2!=lack)
{
printf("-1\n");
return 0;
}
printf("%s\n",ans);
return 0;
}
题意:给定两个字符串s1和s2,求字符串s3使得s3与s1对应位置字符不同的个数为t,并且s3与s2对应位置字符不同的个数也为t。
C. Marina and Vasya
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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
分析:如果构造不出s3,就输出-1。由于找一个与另外两个字符的字符很好找,那么构造不出s3的情况是,s3和s1,s3和s2对应位置相同字符的个数不足。那么最大限度地利用一个位置的贡献就好了,怎么最大限度利用一个位置?当s1[i]==s2[i]的时候,把s3[i]赋成s1[i]。剩下的位置s1和s2交替贡献字符就行了。最后看满不满足条件。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e6;
char s1[maxn],s2[maxn],ans[maxn];
int main()
{
int n,t,i,j,lack;
cin>>n>>t;
cin>>s1>>s2;
lack=n-t;
int cnt1=0,cnt2=0;
memset(ans,'\0',sizeof(ans));
for(i=0;i<n;i++)
{
if(cnt1==lack)
break;
if(s1[i]==s2[i])
{
ans[i]=s1[i];
s1[i]=s2[i]='#';
cnt1++;
cnt2++;
}
}
// printf("%s %s %s\n",ans,s1,s2);
int fg=1;
for(i=0;i<n;i++) if(s1[i]!='#')
{
if(cnt1==lack && cnt2==lack)
break;
if(cnt1==cnt2)
{
ans[i]=s1[i];
cnt1++;
}
else
{
ans[i]=s2[i];
cnt2++;
}
s1[i]=s2[i]='#';
}
// printf("%s %s %s",ans,s1,s2);
for(i=0;i<n;i++) if(s1[i]!='#')
{
for(char ch='a';ch<='z';ch++)
{
if(ch!=s1[i] && ch!=s2[i])
{
ans[i]=ch;
break;
}
}
}
if(cnt1!=lack || cnt2!=lack)
{
printf("-1\n");
return 0;
}
printf("%s\n",ans);
return 0;
}
相关文章推荐
- 1.一些 贪心算法 的简单思维题:
- 贪心算法——字典序最小问题
- 贪心算法——区间调度问题
- Jump Game I,II 贪心
- Wildcard Matching
- 贪心题目循环和控制台折行
- HDOJ 1009
- 【解题报告】【USACO】酸奶工厂
- 【解题报告】【】交谊舞
- POJ 1328
- Best Cow Line
- 贪心 hdu 1003
- 最大容器
- 满足和为定值的两个数或多个数
- HUD1052 __ P2 1002 __ Tian Ji -- The Horse Racing
- 2015年国家集训队测试 BZOJ3816矩阵变幻
- poj 1328
- HDU--1052
- POJ 2709 混合颜料 acm 贪心
- 田忌赛马Tian Ji -- The Horse Racing(hdu 1052)