Codeforces Round #296 (Div. 2) B. Error Correct System
2 seconds
256 megabytes
standard input
standard output
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 T have
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!
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.
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.
9
pergament
permanent
1
4 6
6
wookie
cookie
1
-1 -1
4
petr
egor
2
1 2
6
double
bundle
2
4 1
In the second test it is acceptable to print i = 2, j = 3.
题意:交换S或者T中的两个字符。使得两串的差异度最小。有三种情况,一种是交换后正好两个位置都相应同样,一种是交换后仅仅有一个位置同样,还有就是交换也不能满足相应同样。
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#define N 2222
using namespace std; char s[200009],t[200009];
int n;
int a[N][N]; int main()
{
while(~scanf("%d",&n))
{
scanf("%s %s",s,t); int num=0;
memset(a,0,sizeof a); for(int i=0;i<n;i++)
if(s[i]!=t[i])
{
int x=s[i]-'a';
int y=t[i]-'a';
num++;
a[x][y]=i+1;
} for(int i=0;i<26;i++)//交换后两位置都相应同样了
for(int j=0;j<26;j++)
if(a[i][j] && a[j][i])
{
cout<<num-2<<endl;
cout<<a[i][j]<<" "<<a[j][i]<<endl;
return 0;
} for(int i=0;i<26;i++)//交换后仅仅有一个位置相应同样
for(int j=0;j<26;j++)
if(a[i][j])
{
for(int k=0;k<26;k++)
if(a[j][k])
{
cout<<num-1<<endl;
cout<<a[i][j]<<" "<<a[j][k]<<endl;
return 0;
}
} cout<<num<<endl;
cout<<-1<<" "<<-1<<endl;//无法通过交换降低差异度 }
return 0;
}
Codeforces Round #296 (Div. 2) B. Error Correct System的更多相关文章
- 字符串处理 Codeforces Round #296 (Div. 2) B. Error Correct System
题目传送门 /* 无算法 三种可能:1.交换一对后正好都相同,此时-2 2.上面的情况不可能,交换一对后只有一个相同,此时-1 3.以上都不符合,则不交换,-1 -1 */ #include < ...
- Codeforces Round #296 (Div. 1) C. Data Center Drama 欧拉回路
Codeforces Round #296 (Div. 1)C. Data Center Drama Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xx ...
- CodeForces Round #296 Div.2
A. Playing with Paper 如果a是b的整数倍,那么将得到a/b个正方形,否则的话还会另外得到一个(b, a%b)的长方形. 时间复杂度和欧几里得算法一样. #include < ...
- 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. 1) E. Triangles 3000
http://codeforces.com/contest/528/problem/E 先来吐槽一下,一直没机会进div 1, 马力不如当年, 这场题目都不是非常难,div 2 四道题都是水题! 题目 ...
- 水题 Codeforces Round #296 (Div. 2) A. Playing with Paper
题目传送门 /* 水题 a或b成倍的减 */ #include <cstdio> #include <iostream> #include <algorithm> ...
- Codeforces Round #296 (Div. 2) A. Playing with Paper
A. Playing with Paper One day Vasya was sitting on a not so interesting Maths lesson and making an o ...
- Codeforces Round #296 (Div. 2) A B C D
A:模拟辗转相除法时记录答案 B:3种情况:能降低2,能降低1.不能降低分别考虑清楚 C:利用一个set和一个multiset,把行列分开考虑.利用set自带的排序和查询.每次把对应的块拿出来分成两块 ...
- Codeforces Round #296 (Div. 1) B - Clique Problem
B - Clique Problem 题目大意:给你坐标轴上n个点,每个点的权值为wi,两个点之间有边当且仅当 |xi - xj| >= wi + wj, 问你两两之间都有边的最大点集的大小. ...
随机推荐
- mybatis常用标签
1. 定义sql语句 1.1 select 标签 属性介绍: id :唯一的标识符. parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User或user ...
- [android开发之内容更新类APP]三、项目的基本功能之布局
应用宝的下载地址:http://android.myapp.com/myapp/detail.htm?apkName=com.jov.laughter 其它的市场如木蚂蚁,安卓市场.搜狐也都有了 注: ...
- Makefile生成器,使用C++和Boost实现
今天学习了一下Boost的文件遍历功能,同一时候发现GNU编译器有-MM选项.能够自己主动生成依赖关系,于是利用以上两点写了一个Makefile生成器. 能够生成一般的单个可运行文件的Makefile ...
- Python title() 方法
描述 Python title() 方法返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写. 语法 title() 方法语法: S.title() 参数 无. ...
- Javascript中的对象和原型(一)(转载)
面向对象的语言(如Java)中有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.但是,JavaScript 没有类的概念,因此它的对象也与基于类的语言中的对象有所不同. 要了解面向对象,首 ...
- Nginx+Windows负载均衡(转载)
一.下载Nginxhttp://nginx.org/download/nginx-1.0.8.zip解压到C:\nginx目录下二.在两台服务器上分别建一个网站:S1:192.168.16.35:80 ...
- 使用 hibernate validator 进行表单验证
1 引入依赖,如果是 Maven 项目,仅需要添加如下依赖.官网请查看http://hibernate.org/validator/documentation/getting-started/ < ...
- Object-C中的字符串对象1-不可变字符串
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
- 点滴积累【JS】---JS实现动画闪烁效果
效果: 思路:首先获得图片数组,然后做JS定时用setTimeout和setInterval在用显示隐藏实现闪烁效果. 代码: <!DOCTYPE html PUBLIC "-//W3 ...
- Linux下解压tar.xz
tar xvJf ***.tar.xz 注意零散文件,最好放到文件夹里