Error Correct System CodeForces - 527B
Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings S and T of equal length to be "similar". After a brief search on the Internet, he learned about the Hamming distance between two strings S and T of the same length, which is defined as the number of positions in which S and Thave different characters. For example, the Hamming distance between words "permanent" and "pergament" is two, as these words differ in the fourth and sixth letters.
Moreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn't know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string S, so that the Hamming distance between a new string S and string T would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings.
Help him do this!
Input
The first line contains integer n (1 ≤ n ≤ 200 000) — the length of strings S and T.
The second line contains string S.
The third line contains string T.
Each of the lines only contains lowercase Latin letters.
Output
In the first line, print number x — the minimum possible Hamming distance between strings S and T if you swap at most one pair of letters in S.
In the second line, either print the indexes i and j (1 ≤ i, j ≤ n, i ≠ j), if reaching the minimum possible distance is possible by swapping letters on positions i and j, or print "-1 -1", if it is not necessary to swap characters.
If there are multiple possible answers, print any of them.
Example
9
pergament
permanent
1
4 6
6
wookie
cookie
1
-1 -1
4
petr
egor
2
1 2
6
double
bundle
2
4 1
Note
In the second test it is acceptable to print i = 2, j = 3.
还是万年吐槽,这英语没救了。题目都看不懂的我和瞎子已经没有什么区别了。
题意:
给你两个长度为n的字符串,你有一次机会,将一个字符串的两个字母互换位置。
(要求:要使这两个字符串的元素不同的最少)
如果能互换使得不同之处变少,则输出互换后有多少个不同的元素,
并且输出所交换的元素的位置,如不能则输出-1 -1。
这题是一个非常灵活的思维题,如果思路简单则可以复杂度很小的求出答案。
如果是复杂的思路那就tle了。
这题亮点是用一个二维数组tu[30][30]去维护这两个字符串不同点。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
char a[],b[];
int tu[][];
int main() {
while(scanf("%d%s%s",&n,a,b)!=EOF) {
int sum=;
memset(tu,,sizeof(tu));
for (int i= ; i<n ; i++ ) {
if (a[i]!=b[i]) {
tu[a[i]-'a'][b[i]-'a']=i+;
sum++;
}
}
int flag=;
if (flag) {
for (int i= ; i<n ; i++) {
if (a[i]!=b[i] && tu[b[i]-'a'][a[i]-'a']) {
printf("%d\n%d %d\n",sum-,tu[b[i]-'a'][a[i]-'a'],tu[a[i]-'a'][b[i]-'a']);
flag=;
break;
} }
}
if (flag) {
for (int i= ; i<n ; i++ ) {
if (a[i]!=b[i]) {
for (int j= ; j< ; j++) {
if (tu[b[i]-'a'][j]) {
printf("%d\n%d %d\n",sum-,tu[b[i]-'a'][j],i+);
flag=;
break;
}
}
if (flag==) break;
}
}
}
if (flag) printf("%d\n-1 -1\n",sum);
}
return ;
46}
Error Correct System CodeForces - 527B的更多相关文章
- CodeForces 527B Error Correct System
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- Codeforces Round #296 (Div. 2) B. Error Correct System
B. Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Error Correct System(模拟)
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- CF Error Correct System
Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- B. Error Correct System (CF Round #296 (Div. 2))
B. Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 【codeforces 527B】Error Correct System
[题目链接]:http://codeforces.com/contest/527/problem/B [题意] 给你两个字符串; 允许你交换一个字符串的两个字符一次: 问你这两个字符串最少会有多少个位 ...
- Codeforces Round #296 (Div. 2B. Error Correct System
Ford Prefect got a job as a web developer for a small company that makes towels. His current work ta ...
- 字符串处理 Codeforces Round #296 (Div. 2) B. Error Correct System
题目传送门 /* 无算法 三种可能:1.交换一对后正好都相同,此时-2 2.上面的情况不可能,交换一对后只有一个相同,此时-1 3.以上都不符合,则不交换,-1 -1 */ #include < ...
- codeforce Error Correct System
题目大意: 给出两串n(1 ≤ n ≤ 200 000)个字母的字符串, 求出最多交换一对数, 使得不相同对数变少,求出不相同的对数以及交换的数的位置,若不需交换则输出-1,-1. 分析: 用矩阵记录 ...
随机推荐
- 使用Scanner获取键盘输入
使用Scanner类可以很方便地便获取用户的键盘输入,Scanner是一个基于正则表达式的文本扫描器,它可以从文件.输入流 .字符串中解析出基本类型值和字符串值.Scanner类提供了多个构造器,不同 ...
- LearnPython_week1
1. Python安装 2. Hello World程序 3. 变量的简单使用 4. 注释#'" 5. 用户输入 6. 字符串格式 ...
- 如何在Centos 7上用Logrotate管理日志文件
何为Logrotate? Logrotate是一个实用的日志管理工具,旨在简化对系统上生成大量的日志文件进行管理. Logrotate允许自动旋转压缩,删除和邮寄日志文件,从而节省宝贵的磁盘空间. L ...
- Godep的基本使用
[http://studygolang.com/articles/4385] 关于Godep 发现好多golang项目都使用到godep作为包管理的工具,像比较大型的项目,比如kubernetes这种 ...
- 关于Frame加背景的那点事?
最近新生问我一个问题,继承自Frame(可不是继承自JFrame)的框架怎样添加背景图片, 真够坑的,当时还真懵了,废话少说直接上代码: import java.awt.*; import java. ...
- gerrit+nginx+centos安装配置
安装环境 centos 6.8 gerrit-full-2.5.2.war 下载地址:https://gerrit-releases.storage.googleapis.com/gerrit-ful ...
- Activiti中的各个service的作用
各个Service的作用: RepositoryService 管理流程定义 RuntimeService 执行管理,包括启动.推进.删除流程实例等操作 TaskService 任务管理 Histor ...
- UVA - 1631 Locker 记忆化搜索
题意:给定两个密码串,每次可以让1~3个相邻的密码向上或者向下滚动,每个密码是 ,问最少需要多少次滚动可以让原串成为目标串? 思路:假设当前要让第i位密码还原,我们可以同时转动,不同的转动方式会影响后 ...
- CodeForces - 796C Bank Hacking
思路:共有n-1条边连接n个点,即形成一棵树.一开始需要选择一个点hack--将这个点视为根结点,与它相邻的点防御值加1,与它相隔一个在线点的点的防御也加1.当根节点被hack,即这个点被删除,又变成 ...
- 情景linux--如何解决read命令产生的硬编码问题
情景 我们知道,read命令可以读取文件内容,并把内容赋值给变量. 以如下的数据文件为例. $ cat data.txt 1 201623210021 wangzhiguo 25 2 20162321 ...