Error Correct System

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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!

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 test(s)
Input
9 pergament permanent
Output
1 4 6
Input
6 wookie cookie
Output
1 -1 -1
Input
4 petr egor
Output
2 1 2
Input
6 double bundle
Output
2 4 1

自己尝试写发现很复杂,别人用图来做,甚好。
hamming值最多减2,然后是1,然后是0.
change[i][j]表示需要把i换成j,change[i][j]的值就是此i的位置,那么如果change[i][j]&&change[j][i],就表示需要把i换成j,j换成i,hamming减2.
如果change[i][j] && change[j][k],那么说明i需要换成j,且j在这个位置上也不和另外一个串相等,可以随便换,那么hamming减1.
剩下的情况就是减0了。
 #include <iostream>
using namespace std; int main(void)
{
int change[][] = {};
string s_1,s_2;
int n,hamming = ; cin >> n >> s_1 >> s_2;
for(int i = ;i < n;i ++)
if(s_1[i] != s_2[i])
{
hamming ++;
change[s_1[i] - 'a'][s_2[i] - 'a'] = i + ;
} for(int i = ;i < ;i ++)
for(int j = ;j < ;j ++)
if(change[i][j] && change[j][i])
{
cout << hamming - << endl << change[i][j] << ' ' << change[j][i] << endl;
return ;
} for(int i = ;i < ;i ++)
for(int j = ;j < ;j ++)
for(int k = ;k < ;k ++)
if(change[i][j] && change[j][k])
{
cout << hamming - << endl << change[i][j] << ' ' << change[j][k] << endl;
return ;
}
cout << hamming << endl << "-1 -1" << endl; return ;
}

CF Error Correct System的更多相关文章

  1. 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 ...

  2. CodeForces 527B Error Correct System

    Error Correct System Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  3. Error Correct System(模拟)

     Error Correct System Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 字符串处理 Codeforces Round #296 (Div. 2) B. Error Correct System

    题目传送门 /* 无算法 三种可能:1.交换一对后正好都相同,此时-2 2.上面的情况不可能,交换一对后只有一个相同,此时-1 3.以上都不符合,则不交换,-1 -1 */ #include < ...

  8. codeforce Error Correct System

    题目大意: 给出两串n(1 ≤ n ≤ 200 000)个字母的字符串, 求出最多交换一对数, 使得不相同对数变少,求出不相同的对数以及交换的数的位置,若不需交换则输出-1,-1. 分析: 用矩阵记录 ...

  9. 【codeforces 527B】Error Correct System

    [题目链接]:http://codeforces.com/contest/527/problem/B [题意] 给你两个字符串; 允许你交换一个字符串的两个字符一次: 问你这两个字符串最少会有多少个位 ...

随机推荐

  1. PC问题-该虚拟机似乎正在使用中

    问题现象:运行VMware Workstation,选中一个虚拟机,运行.卡住了,再运行VMware Workstation时,选中一个虚拟机,提示“该虚拟机似乎正在使用中”. 问题原因:因为上次非正 ...

  2. maven系列(1)-maven的介绍与安装

    maven的介绍 maven是大名鼎鼎的Apache下的java构建工具. Apache Maven is a software project management and comprehensio ...

  3. poj 2349 Arctic Network

    http://poj.org/problem?id=2349 Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  4. python challenge第1关--NoteBook上的“乱码”

    在 python challenge第0关中已经得到第1关的地址了: http://www.pythonchallenge.com/pc/def/map.html 一.观察地址栏和标签: What a ...

  5. Sql CLR

    using System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using Microsof ...

  6. UVA1395

    // UVa1395 Slim Span // Rujia Liu #include<cstdio> #include<cmath> #include<cstring&g ...

  7. [0.0]Analysis of Baidu search engine

    Rencently, my two teammates and I is doing a project, a simplified Chinese search engine for childre ...

  8. nrpe 在ubuntu上安装遇到的问题

    Nagios Linux客户端需要安装NRPE进行数据收集,如果在Ubuntu系统下安装过程中遇到下面的错误提示:checking for SSL libraries... configure: er ...

  9. PIL在windwos系统下Image.show无法显示图片问题的解决方法

    环境:1.win7 64位 2.python 2.7.8 3.PIL-1.1.7.win32-py2.7 在运行一下例子时候出现问题: #-*-coding:utf-8-*- __author__ = ...

  10. Java多线程模式(二)

    Guarded Suspension Pattern      该模式描述的是当一个线程在执行某个操作时,但由于其他资源还没有准备好,需要等待,那么就等待资源准备好才开始自己的操作.我们直接看代码例子 ...