Error Correct System

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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 ≤ ni ≠ 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

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

Hint

In the second test it is acceptable to print i = 2, j = 3.

题解:

给两个串,可以交换一次,问不相同字母的对数;模拟,vis1,vis2存储不匹配字母个数;num1,num2存储对应的下标;

对于每个不匹配的字母;在另一个串中找判断对应是否相同;

代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
using namespace std;
typedef long long LL;
const int MAXN = ;
char s1[MAXN], s2[MAXN];
int vis1[], vis2[];
int num1[][MAXN], num2[][MAXN];
map<int, int>mp;
int main(){
int N;
while(~scanf("%d", &N)){
scanf("%s%s", s1 + , s2 + );
int num = ;
memset(vis1, , sizeof(vis1));
memset(vis2, , sizeof(vis2));
memset(num1, , sizeof(num1));
memset(num2, , sizeof(num2));
for(int i = ; i <= N; i++){
if(s1[i] != s2[i]){
vis1[s1[i] - 'a']++;
num1[s1[i] - 'a'][vis1[s1[i] - 'a']] = i;
vis2[s2[i] - 'a']++;
num2[s2[i] - 'a'][vis2[s2[i] - 'a']] = i;
num++;
}
}
int ans = , pos, flot = , l = , r = ;
for(int i = ; i < ; i++){
if(vis1[i]){
if(vis2[i]){
mp.clear();
for(int j = ; j <= vis2[i]; j++){
pos = num2[i][j];
mp[s1[pos] - 'a'] = pos;
l = pos;r = num1[i][];
}
for(int j = ; j <= vis1[i]; j++){
pos = num1[i][j];
if(mp.count(s2[pos] - 'a')){
l = pos; r = mp[s2[pos] - 'a'];
ans = max(ans, );
flot = ;
break;
}
}
if(flot)break; ans = max(ans, );
}
}
}
if(ans == )
for(int i = ; i < ; i++){
if(vis2[i]){
if(vis1[i]){
mp.clear();
for(int j = ; j <= vis1[i]; j++){
pos = num1[i][j];
mp[s1[pos] - 'a'] = pos;
l = pos;r = num2[i][];
}
for(int j = ; j <= vis2[i]; j++){
pos = num2[i][j];
if(mp.count(s1[pos] - 'a')){
l = pos; r = mp[s1[pos] - 'a'];
ans = max(ans, );
flot = ;
break;
}
}
if(flot)break; ans = max(ans, );
}
}
} printf("%d\n", num - ans);
if(l == && r == )puts("-1 -1");
else printf("%d %d\n", l, r);
}
return ;
}

Error Correct System(模拟)的更多相关文章

  1. CodeForces 527B Error Correct System

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

  2. CF Error Correct System

    Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

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

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

  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. Android系统匿名共享内存Ashmem(Anonymous Shared Memory)驱动程序源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6664554 在上一文章Android系统匿名共 ...

  2. Dalvik虚拟机简要介绍和学习计划

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8852432 我们知道,Android应用程序是 ...

  3. Tomcat 配置篇

    Tomcat 配置一.Tomcat 基本介绍 1.关键目录 a) bin 该目录包含了启动.停止和启动其他的脚本,如startup.sh.shutdown.sh等; b) conf 配置文件和一些文档 ...

  4. [Python学习笔记][Python内置函数]

    Python 常用内建函数 比较基础的列表 abs(x) 求绝对值 pow(x,y) 返回x的y次方,等同于x**y round(x[,小数位数]) 对x进行四舍五入,若不指定位数,则返回整数 chr ...

  5. js判断ie浏览器

    function isIE() { //ie? if (!!window.ActiveXObject || "ActiveXObject" in window){ document ...

  6. 提示框的优化之自定义Toast组件之(三)Toast组件优化

    开发步骤: 在toast_customer.xml文件中添加一个图片组件对象显示提示图片 <?xml version="1.0" encoding="utf-8&q ...

  7. 基础命名空间:反射 using System.Reflection

    反射概念:       .Net的应用程序由几个部分:‘程序集(Assembly)’.‘模块(Module)’.‘类型(class)’组成,程序集包含模块 模块包含类型,类型又包含 成员,而反射提供一 ...

  8. iOS 中的正则匹配(工具类方法)

    正则表达式 正则表达式是对字符串操作的一种逻辑公式, 用事先定义好的一些特定字符.及这些特定字符的组合, 组成一个"规则字符串", 这个"规则字符串"用来表达对 ...

  9. hdu1754 基础线段树

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  10. self parent $this关键字分析--PHP

    self :    调用本类的静态方法和属性,常量 parent :调用父类的静态方法.属性.普通方法.构造函数,不能调用父类的普通属性 $this :    调用本类的普通方法和属性,如果本类没有就 ...