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 ...
随机推荐
- iOS开发Block的使用
Block 是从 iOS4引入的,在日常开发中,会经常用到Block.特别是在多线程中,Block的用处更广泛.而且,Block不仅可以接收参数,其本身也可以作为参数,因此,Block的功能非常强大. ...
- 【UML】——为什么要使用UML
以前一提到UML,就想到了复杂的流程图.很敬佩哪些想想就能画出整个系统的UML图的人,因为他们头脑中有整个软件架构的蓝图,这样在编写实现的时候,就会知道哪个地方改怎么做,哪个地方如何扩展. 而想成为架 ...
- iOS动画详解(二)
UIImage常用的绘图操作 一个UIImage对象提供了向当前上下文绘制自身的方法.我们现在已经知道如何获取一个图片类型的上下文并将它转变成当前上下文. 平移操作:下面的代码展示了如何将UI ...
- MATLAB学习拾遗
1.坐标轴修饰 axis equal:axis([0,6,0,6]) 不修饰则为默认网格 grid on 2.不太漂亮的pretty命令 3. Laplace变换 syms t s a b f1=ex ...
- Android动画Animation之Tween用代码实现动画
透明度动画.旋转动画.尺寸伸缩动画.移动动画 package com.javen.tween; import android.annotation.SuppressLint; import andro ...
- C#WinForm应用程序实现自动填充网页上的用户名和密码并点击登录按钮【转载】
使用WebBrowser控件,在documentComplete事件处理器里写 HtmlElement name = webBrowser1.Document.GetElementById(" ...
- SQL Server 2008中的hierarchyid
这也是SQL Server 2008的一个重要新增特性.主要解决的问题是拥有层次关系的表格.例如我们日常生活中用到最多的组织结构图.我们一般会用一个Region表保存区域数据,而每个区域则又可能会有相 ...
- CodeMachine Debugger Extension DLL
http://www.codemachine.com/downloads.html http://www.codemachine.com/tool_cmkd.html#stack
- string中find函数的使用
9.47 编写程序,首先查找string"ab2c3d7R4E6"中的每个数字字符,然后查找其中每个字母字符.编写两个版本的程序,第一个要使用find_first_of,第二个要使 ...
- ajax 用xml传递数据
页面代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx. ...