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 ...
随机推荐
- PowerDesigner 模型文档 说明
PowerDesigner 模型文档 说明 目录(?)[+] 一. 模型文档说明 在前面几篇里介绍了PowerDesigner 的几种模型,如果我们项目里用到的模型较多,亦或者项目牵涉的部门很 ...
- MFC 学习之 鼠标移动到Toolbar按钮上显示提示信息(tooltip),状态栏也随之改变
1.在ResourceView里加入Toolbar资源,命名IDR_TOOLBAR1 2.在主程序的.h文件中加入变量: CToolBar m_toolbar;CImageList ...
- 【Android 应用开发】Activity 状态保存 OnSaveInstanceState參数解析
作者 : 韩曙亮 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/38297083 一. 相关方法简单介绍 1. 状态保存方法演示 ...
- 如何使得VIM显示行号
如何使得VIM显示行号: :set number
- Nginx动静分离经典
Nginx:安装nginx之前需要安装pcre包和zlib以支持重写,正则以及网页压缩等等]把所需的包下载到/usr/src下[根据自己的习惯,路径可以改变]1.首先安装pcre: cd /usr/s ...
- careercup-中等难题
17.1 编写一个函数,不用临时变量,直接交换两函数. 解法: 方法一:这个是经典面试题,也相当直接.我们将用a0表示a的初值,b0表示b的初始值,用diff表示a0-b0的值. 让我们将a>b ...
- [C++]对象的销毁机制
销毁时会按照从后向前的顺序销毁,也就是说,越在后面定义的对象会越早销毁.其中的原因就是函数是在栈中保存的,因此,先定义的对象先压栈,所以在退栈时就会后销毁.而如果参数有多个的话,大多数编译器是从右开始 ...
- Cookie API
Cookie API All cookies created by the Nova framework are encrypted and signed with an authentication ...
- CSS3 动态魔方的展示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android_Notification
xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...