zoukankan      html  css  js  c++  java
  • Error Correct System CodeForces

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

    Example

    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

    Note

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

      还是万年吐槽,这英语没救了。题目都看不懂的我和瞎子已经没有什么区别了。

      题意:

             给你两个长度为n的字符串,你有一次机会,将一个字符串的两个字母互换位置。

            (要求:要使这两个字符串的元素不同的最少)

            如果能互换使得不同之处变少,则输出互换后有多少个不同的元素,

            并且输出所交换的元素的位置,如不能则输出-1 -1。

    这题是一个非常灵活的思维题,如果思路简单则可以复杂度很小的求出答案。

    如果是复杂的思路那就tle了。

    这题亮点是用一个二维数组tu[30][30]去维护这两个字符串不同点。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 int n;
     6 char a[200010],b[200010];
     7 int tu[30][30];
     8 int main() {
     9     while(scanf("%d%s%s",&n,a,b)!=EOF) {
    10         int sum=0;
    11         memset(tu,0,sizeof(tu));
    12         for (int i=0 ; i<n ; i++ ) {
    13             if (a[i]!=b[i]) {
    14                 tu[a[i]-'a'][b[i]-'a']=i+1;
    15                 sum++;
    16             }
    17         }
    18         int flag=1;
    19         if (flag) {
    20             for (int i=0 ; i<n ; i++) {
    21                 if (a[i]!=b[i] && tu[b[i]-'a'][a[i]-'a']) {
    22                     printf("%d
    %d %d
    ",sum-2,tu[b[i]-'a'][a[i]-'a'],tu[a[i]-'a'][b[i]-'a']);
    23                     flag=0;
    24                     break;
    25                 }
    26 
    27             }
    28         }
    29         if (flag) {
    30             for (int i=0 ; i<n ; i++ ) {
    31                 if (a[i]!=b[i]) {
    32                     for (int j=0 ; j<30  ; j++) {
    33                         if (tu[b[i]-'a'][j]) {
    34                             printf("%d
    %d %d
    ",sum-1,tu[b[i]-'a'][j],i+1);
    35                             flag=0;
    36                             break;
    37                         }
    38                     }
    39                     if (flag==0) break;
    40                 }
    41             }
    42         }
    43         if (flag) printf("%d
    -1 -1
    ",sum);
    44     }
    45     return 0;
    46}
  • 相关阅读:
    Python爬虫框架Scrapy
    继承
    c/c++面试题(7)零碎知识总结
    Linux网络编程(多人在线聊天系统)
    第一章 计算机系统漫游
    Linux网络编程(简单的时间获取服务器)
    虚函数(1)
    c/c++面试题(9)linux方向
    Linux网络编程的一般步骤(1)
    进程/线程介绍
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/8510868.html
Copyright © 2011-2022 走看看