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

Hint

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

题解:

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

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

代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<cstdio>
  6. #include<map>
  7. using namespace std;
  8. typedef long long LL;
  9. const int MAXN = ;
  10. char s1[MAXN], s2[MAXN];
  11. int vis1[], vis2[];
  12. int num1[][MAXN], num2[][MAXN];
  13. map<int, int>mp;
  14. int main(){
  15. int N;
  16. while(~scanf("%d", &N)){
  17. scanf("%s%s", s1 + , s2 + );
  18. int num = ;
  19. memset(vis1, , sizeof(vis1));
  20. memset(vis2, , sizeof(vis2));
  21. memset(num1, , sizeof(num1));
  22. memset(num2, , sizeof(num2));
  23. for(int i = ; i <= N; i++){
  24. if(s1[i] != s2[i]){
  25. vis1[s1[i] - 'a']++;
  26. num1[s1[i] - 'a'][vis1[s1[i] - 'a']] = i;
  27. vis2[s2[i] - 'a']++;
  28. num2[s2[i] - 'a'][vis2[s2[i] - 'a']] = i;
  29. num++;
  30. }
  31. }
  32. int ans = , pos, flot = , l = , r = ;
  33. for(int i = ; i < ; i++){
  34. if(vis1[i]){
  35. if(vis2[i]){
  36. mp.clear();
  37. for(int j = ; j <= vis2[i]; j++){
  38. pos = num2[i][j];
  39. mp[s1[pos] - 'a'] = pos;
  40. l = pos;r = num1[i][];
  41. }
  42. for(int j = ; j <= vis1[i]; j++){
  43. pos = num1[i][j];
  44. if(mp.count(s2[pos] - 'a')){
  45. l = pos; r = mp[s2[pos] - 'a'];
  46. ans = max(ans, );
  47. flot = ;
  48. break;
  49. }
  50. }
  51. if(flot)break;
  52.  
  53. ans = max(ans, );
  54. }
  55. }
  56. }
  57. if(ans == )
  58. for(int i = ; i < ; i++){
  59. if(vis2[i]){
  60. if(vis1[i]){
  61. mp.clear();
  62. for(int j = ; j <= vis1[i]; j++){
  63. pos = num1[i][j];
  64. mp[s1[pos] - 'a'] = pos;
  65. l = pos;r = num2[i][];
  66. }
  67. for(int j = ; j <= vis2[i]; j++){
  68. pos = num2[i][j];
  69. if(mp.count(s1[pos] - 'a')){
  70. l = pos; r = mp[s1[pos] - 'a'];
  71. ans = max(ans, );
  72. flot = ;
  73. break;
  74. }
  75. }
  76. if(flot)break;
  77.  
  78. ans = max(ans, );
  79. }
  80. }
  81. }
  82.  
  83. printf("%d\n", num - ans);
  84. if(l == && r == )puts("-1 -1");
  85. else printf("%d %d\n", l, r);
  86. }
  87. return ;
  88. }

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. python urllib基础学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #使用python创建一个简单的WEB客户端 import urll ...

  2. PHP学习笔记三十【final】

    <?php //final不能去修饰属性(变量) //如果希望类不希望被继承就可以使用final关键字 final class Person() { public function sayHi( ...

  3. Webform Lable

  4. POJ1275/ZOJ1420/HDU1529 Cashier Employment (差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 题意:一商店二十四小时营业,但每个时间段需求的出纳员不同,现有n个人申请这份工作, ...

  5. (转)matlab 字符串处理函数

    转自:http://www.cnblogs.com/emanlee/archive/2012/09/13/2683912.html % 字符串处理 a='  a';b='b  b';c='cccc'; ...

  6. 关于"zoom“ 的一点小认识

    最早接触zoom是在清除浮动的时候,原因就是zoom能触发IE的haslayout,当时也没深究其原理,今天,在查看张鑫旭的对overflow与zoom”清除浮动”的一些认识时,其中提到zoom是比例 ...

  7. mysql数据库的连接

    public TJb checkjbByschool(long id)throws ClassNotFoundException,SQLException { Class.forName(" ...

  8. Android网络连接的两种方法:apache client和httpurlconnection的比较

    另见http://blog.csdn.net/mingli198611/article/details/8766585 在官方blog中,android工程师谈到了如何去选择apache client ...

  9. bootstrap data- jquery .data

    jquery官网对.data函数描述是:在匹配元素上存储任意相关数据 或 返回匹配的元素集合中的第一个元素的给定名称的数据存储的值. 存储键值(key/value): $("body&quo ...

  10. python快速搭建WebServer

    #!/usr/bin/python import SimpleHTTPServer import SocketServer import os PORT = 7777 WEBDIR = "/ ...