原题链接:http://codeforces.com/contest/584/problem/C

题意:

定义$f(s1,s2)$为$s1,s2$不同的字母的个数。现在让你构造一个串$s3$,使得$f(s1,s3)=f(s2,s3)=t$。

题解:

设$s1,s2$共有$a$个相同的字母,共有$b$个不同的字母。现在在这$a$个相同的字母中,我让$s3$有$x$个字母和$s1,s2$不同;在这$b$个不同的字母中,我让$s3$有$y$个字母和$s1,s2$都不相同,有$z$个字母和$s1$不同。则我们可以得到以下的约束关系:

$$f(s1,s3)=x+y+z=t$$

$$f(s2,s3)=x+y+(b-y-z)=x+b-z=t$$

$$x \in [0,a]$$

$$y \in [0,b]$$

$$z \in [0,b]$$

$$y+z \in [0,b]$$

然后通过枚举$x$,检查$y,z$就能得到答案了。

代码:

#include<iostream>
#include<cstring>
#include<string>
#define MAX_N 100005
using namespace std; int n,t;
string s1,s2; int ans[MAX_N]; int main() {
cin.sync_with_stdio(false);
cin >> n >> t >> s1 >> s2;
int a = , b = ;
for (int i = ; i < n; i++) {
a += (s1[i] == s2[i]);
b += (s1[i] != s2[i]);
}
for (int x = ; x <= a; x++) {
int z = b + x - t;
int y = t - z - x;
if (z < || z > b || y < || y > b || y + z > b)continue;
for (int i = ; i < n; i++) {
if (s1[i] == s2[i]) {
if (x)ans[i] = (s1[i] == 'z' ? 'a' : s1[i] + ), x--;
else ans[i] = s1[i];
}
else {
if (y) {
int tmp = 'a';
while (tmp == s1[i] || tmp == s2[i])tmp++;
ans[i] = tmp;
y--;
}
else if (z)ans[i] = s1[i], z--;
else ans[i] = s2[i];
}
}
for (int i = ; i < n; i++)cout << (char) ans[i];
cout << endl;
return ;
}
cout << - << endl;
return ;
}

Codeforces Round #324 (Div. 2) Marina and Vasya 乱搞推理的更多相关文章

  1. Codeforces Round #323 (Div. 2) D. Once Again... 乱搞+LIS

    D. Once Again... time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. Codeforces Round #324 (Div. 2)解题报告

    ---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...

  3. Codeforces Round #324 (Div. 2) C. Marina and Vasya 贪心

    C. Marina and Vasya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pr ...

  4. Codeforces Round #324 (Div. 2)C. Marina and Vasya set

                                                          C. Marina and Vasya   Marina loves strings of ...

  5. Codeforces Round #281 (Div. 2) A. Vasya and Football 模拟

    A. Vasya and Football 题目连接: http://codeforces.com/contest/493/problem/A Description Vasya has starte ...

  6. Codeforces Round #324 (Div. 2)

    CF的rating设置改了..人太多了,决定开小号打,果然是明智的选择! 水 A - Olesya and Rodion #include <bits/stdc++.h> using na ...

  7. Codeforces Round #324 (Div. 2)C. Marina and Vasya

    A的万般无奈...后来跑了大牛的这份代码发现, 题意是求一个序列与给定的两个序列有t个不同. 只要保证...对应位置就行了.. 所以处理起来非常方便.............. 可是没有感觉是对应位置 ...

  8. Codeforces Round #324 (Div. 2) C (二分)

    题目链接:http://codeforces.com/contest/734/problem/C 题意: 玩一个游戏,一开始升一级需要t秒时间,现在有a, b两种魔法,两种魔法分别有m1, m2种效果 ...

  9. Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心

    E. Anton and Ira Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...

随机推荐

  1. IOS开发---菜鸟学习之路--(二十四)-iOS7View被导航栏遮挡问题的解决

    (此文为复制帖,原文地址为:http://blog.sina.com.cn/s/blog_a8192bdd0101af40.html) self.navigationController.naviga ...

  2. Windows网络编程笔记6 --- WinSock I/O 控制方法

    Windows提供了两种方式“套接字模式”和“套接字I/O模型”,可对一个套接字上的I/O行为加以控制.套接字模式用于决定在随一个套接字调用时,那些 Winsock函数的行为.其中的模型包括括sele ...

  3. 深入学习之mysql(一)数据库操作

    1.显示所有数据库: SHOW DATABASES; 2.创建数据库: CREATE DATABASE 数据库名: 3.选择你所创建的数据库: USE 数据库名; 4.删除数据库: DROP 数据库名 ...

  4. python中 in, any 和 all用法

    in if x == 1 or y == 1 or z == 1: print('passed') if 1 in (x, y, z): print('passed') any if x or y o ...

  5. android下拉弹出动画

    <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http:// ...

  6. oracle中用rownum分页并排序的查询SQL语句

    oracle的sql语句中没有limit,limit是mysql中特有的,在oracle中可用rownum来表示,用于查询结果中的前N行数据. 如要查询emp表中的前5行数据,可用如下语句: sele ...

  7. 【bzoj2431】[HAOI2009]逆序对数列 dp

    题目描述 对于一个数列{ai},如果有i<j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的 数列,可以很容易求出有多少个逆序对数.那么逆序对数为k的这 ...

  8. CMake 使用笔记

    记录 CMake 相关知识. Prelude:读文档一定要有耐心! 问题一 CLion: CMakeLists.txt 中 set(CMAKE_CXX_FLAGS -Wall) 不起作用 Soluti ...

  9. BZOJ2395 [Balkan 2011]Timeismoney 【最小乘积生成树】

    题目链接 BZOJ2395 题意:无向图中每条边有两种权值,定义一个生成树的权值为两种权值各自的和的积 求权值最小的生成树 题解 如果我们将一个生成树的权值看做坐标,那么每一个生成树就对应一个二维平面 ...

  10. Educational Codeforces Round 8 B 找规律

    B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...