题意:f(a,b)表示字符串a和b的不同字符个数。求f(a,c)=f(b,c)=t的字符串C
分两种情况,我这定义的是f(a,b)表示字符串a和b的相同字符个数, f(a,b)>=n-t和其他
第一种情况一定可以,留下n-t个相同的字符,其他的使其与字符串a,b都不同
第二种情况一部分可以,留下f(a,b)个相同的字符,再让n-t-f(a,b)个字符与a相同但与b不同,让n-t-f(a,b)个字符与b相同但与a不同
#include
#include
#include
#include
using namespace std;
const int N = 100001;
char str2[N];
char str1[N];
bool flag[N];
char str3[N];
char a[] = "abcdefg";
int f(int n, char str1[], char str2[])
{
int i;
int ans = 0;
for(i = 0; i < n; i++)
{
if(str1[i] == str2[i])
{
flag[i] = 1;
ans++;
}
}
return ans;
}
int main(void)
{
int n, t;
while(~scanf("%d%d", &n, &t))
{
int i, j;
memset(flag, 0, sizeof(flag));
memset(str3, 0, sizeof(str3));
getchar();
scanf("%s%s", str1, str2);
int tmp = f(n, str1, str2);
if(tmp >= n-t)
{
int k;
for(k = 0,i = 0; i < n; i++)
{
if(flag[i] && k < n-t)//tong
{
k++;
str3[i] = str1[i];
}
else
{
for(j = 0; j < 3; j++)
{
if(str1[i] != a[j] && str2[i] != a[j])
{
str3[i] = a[j];
break;
}
}
}
}
}
else
{
int k = 0;
int j_1 = 0, k_1 = 0;
for(j = 0,i = 0; i < n; i++)
{
if(flag[i])
{
str3[i] = str1[i];
j++;
k++;
}
else if(j_1 < n-t-tmp && str1[i] != str2[i])
{
str3[i] = str1[i];
j_1++;
j++;
}
else if(k_1 < n-t-tmp && str1[i] != str2[i])
{
str3[i] = str2[i];
k_1++;
k++;
}
else
{
for(int cnt = 0; cnt < 3; cnt++)
{
if(str1[i] != a[cnt] && str2[i] != a[cnt])
{
str3[i] = a[cnt];
break;
}
}
}
}
}
if(f(n, str3, str2) == f(n, str3, str2) && f(n, str3, str2) == n-t)
{
printf("%s\n", str3);
}
else
printf("-1\n");
}
return 0;
}