CF Error Correct System
Error Correct System
2 seconds
256 megabytes
standard input
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!
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.
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.
9 pergament permanent
1 4 6
6 wookie cookie
1 -1 -1
4 petr egor
2 1 2
6 double bundle
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的更多相关文章
- 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 ...
- CodeForces 527B Error Correct System
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- Error Correct System(模拟)
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- 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 ...
- 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 ...
- 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 ...
- 字符串处理 Codeforces Round #296 (Div. 2) B. Error Correct System
题目传送门 /* 无算法 三种可能:1.交换一对后正好都相同,此时-2 2.上面的情况不可能,交换一对后只有一个相同,此时-1 3.以上都不符合,则不交换,-1 -1 */ #include < ...
- codeforce Error Correct System
题目大意: 给出两串n(1 ≤ n ≤ 200 000)个字母的字符串, 求出最多交换一对数, 使得不相同对数变少,求出不相同的对数以及交换的数的位置,若不需交换则输出-1,-1. 分析: 用矩阵记录 ...
- 【codeforces 527B】Error Correct System
[题目链接]:http://codeforces.com/contest/527/problem/B [题意] 给你两个字符串; 允许你交换一个字符串的两个字符一次: 问你这两个字符串最少会有多少个位 ...
随机推荐
- 值栈与ognl
ValueStack (值栈): 1.贯穿整个Action的生命周期(每个Action类的对象实例都拥有一个ValueStack对象).相当于一个数据的中转站.在其中保存当前Action对象和其他相关 ...
- BestCoder Round #66 (div.2) hdu5592
GTW likes math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- Eclipse查找类路径快捷方式
直接ctrl+shift+t查找这个类,下面会显示类的路径,包括jar名
- angular 管理后台
http://blog.csdn.net/iamnieo/article/details/50474399
- Vieta定理
一元$n$次方程$$P(x)=a_{n}x^{n}+a_{n-1}x^{n-1}+\cdots+a_{a}x+a_{0}=a_{n}(x-x_{1})(x-x_{2})\cdots (x-x_{n}) ...
- Python3批量爬取网页图片
所谓爬取其实就是获取链接的内容保存到本地.所以爬之前需要先知道要爬的链接是什么. 要爬取的页面是这个:http://findicons.com/pack/2787/beautiful_flat_ico ...
- C# is与as
1.使用场景: 强制类型转换,有可能会导致异常.is与as就是为了解决这一问题,is与as永远不会抛出异常. 2.is判断一个对象是否兼容于指定的类型,考虑里氏代换.Dog是Animal,而Anima ...
- 一条直线上N个线段所覆盖的总长度
原文:http://blog.csdn.net/bxyill/article/details/8962832 问题描述: 现有一直线,从原点到无穷大. 这条直线上有N个线段.线段可能相交. 问,N个线 ...
- Redis 客户端连接
Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接,当一个连接建立后,Redis 内部会进行以下一些操作: 首先,客户端 socket 会被设置为非阻 ...
- Linux 学习笔记 Linux环境变量初稿
set命令会显示为某个特定进程设置的所有环境变量,但不包括一些没有设置值的默认环境变量 可以通过等号来给环境变量赋值,值可以是数值或字符串 如果要给变量赋一个含有空格的字符串值,必需用单引号来界定字符 ...