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

    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了。
     1 #include <iostream>
     2 using    namespace    std;
     3 
     4 int    main(void)
     5 {
     6     int    change[30][30] = {0};
     7     string    s_1,s_2;
     8     int    n,hamming = 0;
     9 
    10     cin >> n >> s_1 >> s_2;
    11     for(int i = 0;i < n;i ++)
    12         if(s_1[i] != s_2[i])
    13         {
    14             hamming ++;
    15             change[s_1[i] - 'a'][s_2[i] - 'a'] = i + 1;
    16         }
    17 
    18     for(int i = 0;i < 26;i ++)
    19         for(int j = 0;j < 26;j ++)
    20             if(change[i][j] && change[j][i])
    21             {
    22                 cout << hamming - 2 << endl << change[i][j] << ' ' << change[j][i] << endl;
    23                 return    0;
    24             }
    25 
    26     for(int i = 0;i < 26;i ++)
    27         for(int j = 0;j < 26;j ++)
    28             for(int k = 0;k < 26;k ++)
    29                 if(change[i][j] && change[j][k])
    30                 {
    31                     cout << hamming - 1 << endl << change[i][j] << ' ' << change[j][k] << endl;
    32                     return    0;
    33                 }
    34     cout << hamming << endl << "-1 -1" << endl;
    35 
    36     return    0;
    37 }
  • 相关阅读:
    MySQL 对于千万级的大表要怎么优化?
    随便写的一些docker使用操作命令
    零基础学python大概要多久?我用了30天
    普通人学python有意义吗?意义重大
    华为私有云组件
    Mysql 调优(二)分析思路
    MySQL 调优(一)调优原则
    shell脚本获取当前时间,分钟之前时间、小时之前时间和天之前时间
    java_windows环境变量自动设置脚本
    plsql中文乱码问题解决方案
  • 原文地址:https://www.cnblogs.com/xz816111/p/4394658.html
Copyright © 2011-2022 走看看