CodeForces 527B
Description
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!
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.
Sample Input
9
pergament
permanent
1
4 6
6
wookie
cookie
1
-1 -1
4
petr
egor
2
1 2
6
double
bundle
2
4 1
Sample Output
Hint
In the second test it is acceptable to print i = 2, j = 3.
解题思路:
题目大意:给两个长度相等的字符串,统计不同字符位置的个数,同时允许交换一组字符位置,使字符串相似度最高,要求输出交换后的距离(不同个数),以及交换的位置,如果没有交换的必要则输出-1 -1。
用一个二维数组存储两不同字符的位置:Map[i][j]=loca(loca位置的俩字符i与j不同)
有三种情况:交换一次,交换的两个位置都完全匹配;交换一次,一个位置匹配;不交换。
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
#define Max 200005
int Map[][];
int p1=-,p2=-;
int main()
{
string a,b;
int n,count=;
cin>>n;
cin>>a>>b;
memset(Map,-,sizeof(Map));
for(int j=;j<n;j++)
{
if(a[j]!=b[j])
{
Map[a[j]-'a'][b[j]-'a']=j;
count=count+;
}
}
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(Map[i][j]!=-&&Map[j][i]!=-)
{
p1=Map[i][j]+;
p2=Map[j][i]+;
count=count-;
printf("%d\n",count);
printf("%d %d\n",p1,p2);
return ;
}
}
}
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
if(Map[i][j]!=-&&Map[j][k]!=-)
{
p1=Map[i][j]+;
p2=Map[j][k]+;
count=count-;
printf("%d\n",count);
printf("%d %d\n",p1,p2);
return ;
}
}
}
}
printf("%d\n",count);
printf("%d %d\n",p1,p2);
return ;
}
CodeForces 527B的更多相关文章
- CodeForces 527B Error Correct System
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- 【codeforces 527B】Error Correct System
[题目链接]:http://codeforces.com/contest/527/problem/B [题意] 给你两个字符串; 允许你交换一个字符串的两个字符一次: 问你这两个字符串最少会有多少个位 ...
- Error Correct System CodeForces - 527B
Ford Prefect got a job as a web developer for a small company that makes towels. His current work ta ...
- Error Correct System(模拟)
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- Codeforces Round #527-B. Teams Forming(贪心)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
随机推荐
- IEnumerable、IEnumerator与yield的学习
我们知道数组对象可以使用foreach迭代进行遍历,同时我们发现类ArrayList和List也可以使用foreach进行迭代.如果我们自己编写的类也需要使用foreach进行迭代时该怎么办呢? IE ...
- java System 常用方法
一.System.currentTimeMillis() 获取系统当前时间,毫秒 二.System.getProperty Java.version Java 运行时环境版本 java.vendor ...
- centos 火狐浏览器安装adobe flash player插件
来个CentOS 6.3 为Firefox安装Flash插件教程 下载install_flash_player_11_linux.i386.tar.gz打包文件到本地 终端进入install_fl ...
- PHP中各种Hash算法性能比较
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 声明:function FileSetAttr ( const FileName : string
对文件和文件夹都有效 FileSetAttr('D:\Administrator\Desktop\patcher\Win32\Release\config\element\update',faHid ...
- JS 实现2+2=5的代码 实现原理解析
当我在http://segmentfault.com/q/1010000000531302 看到的时候 先是一惊. 这是代码 g = function () { H = 3 return H ...
- Tomcat部署web应用的三种方式
原文:http://my.oschina.net/sunchp/blog/90235 一:相关概念 CATALINA_HOME:tomcat安装目录 CATALINA_BASE:tomcat工作目录 ...
- android学习日记15--WebView(网络视图)
一.WebView 1.简述 WebView(网络视图)内置WebKit引擎,能加载显示网页,还支持JS,并且能够在Android平台使用AJAXWebView可以在布局中声明,也可以在Activit ...
- python的内存管理机制 图解+Django Web开发学习笔记
http://www.cnblogs.com/CBDoctor/p/3781078.html http://www.cnblogs.com/vamei/p/3232088.html http://bl ...
- Array.prototype.slice.call(arguments) 类数组转成真正的数组
Array.prototype.slice.call(arguments) 我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数 ...