B. Error Correct System (CF Round #296 (Div. 2))
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
In the second test it is acceptable to print i = 2, j = 3.
先用一个cnt[i][j]数组记录不相等时的位置。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#define maxn 200005
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
typedef long long ll;
using namespace std; char s1[maxn],s2[maxn];
int cnt[30][30];
int n; int main()
{
int i,j,k;
while (~sf(n))
{
FRL(i,0,30)
FRL(j,0,30)
cnt[i][j]=0;
scanf("%s%s",s1,s2);
int ans=0;
FRL(i,0,n)
{
if (s1[i]!=s2[i])
{
ans++; //总的不相等的个数
cnt[s1[i]-'a'][s2[i]-'a']=i+1;
}
}
int num=0,s=-1,t=-1;
bool flag=false;
FRL(i,0,26)
{
FRL(j,0,26)
{
if (i!=j)
{
if (cnt[i][j]>0) //cnt[i][j]位置不相等('a'+i相应着'a'+j)
{
if (cnt[j][i]>0) //看是否有某个位置'a'+j相应着'a'+i,有的话就交换
{
s=cnt[i][j];
t=cnt[j][i];
num=2;
flag=true;
break;
}
else
{
FRL(k,0,26)
{
if (cnt[k][i]>0)
{
s=cnt[i][j];
t=cnt[k][i];
num=1;
}
}
}
}
}
}
if (flag) break;
}
pf("%d\n%d %d\n",ans-num,s,t);
}
return 0;
}
B. Error Correct System (CF Round #296 (Div. 2))的更多相关文章
- C. Glass Carving (CF Round #296 (Div. 2) STL--set的运用 && 并查集方法)
C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 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 ...
- CF Error Correct System
Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 527B Error Correct System
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- CodeForces Round #296 Div.2
A. Playing with Paper 如果a是b的整数倍,那么将得到a/b个正方形,否则的话还会另外得到一个(b, a%b)的长方形. 时间复杂度和欧几里得算法一样. #include < ...
- Error Correct System(模拟)
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- CF Round #551 (Div. 2) D
CF Round #551 (Div. 2) D 链接 https://codeforces.com/contest/1153/problem/D 思路 不考虑赋值和贪心,考虑排名. 设\(dp_i\ ...
- CF Round #510 (Div. 2)
前言:没想到那么快就打了第二场,题目难度比CF Round #509 (Div. 2)这场要难些,不过我依旧菜,这场更是被\(D\)题卡了,最后\(C\)题都来不及敲了..最后才\(A\)了\(3\) ...
- Codeforces Round #296 (Div. 1) C. Data Center Drama 欧拉回路
Codeforces Round #296 (Div. 1)C. Data Center Drama Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xx ...
随机推荐
- 自动交互脚本之expect使用记录
之前一直没怎么用这个命令,意外用了一下,还不错,那这个是干嘛的呢 我们或多或少会远程登录其他服务器,需要执行某项任务,通常需要手动接入,输入密码啊,等等 那我们如何有效的自动执行呢,expect可以解 ...
- mathAge.call(btn) 函数call 改变函数内 this #js
mathAge.call(btn) 函数call 改变函数内 this
- wpf Command 携带当前窗口
Command="{Binding GoPayCommand}" CommandParameter="{Binding RelativeSource={RelativeS ...
- 利用jQuery对li标签操作
<ul class="con" id="products"> <li i=" class=""> < ...
- CF1000G Two-Paths
题目大意:给你一棵树,其中点上和边上都有值.定义2-Path为经过一条边最多两次的路径,价值为经过点的权值加和-经过边权值*该边经过次数.4e5组询问,每次询问树上连接x,y两点的2-Path的最大价 ...
- 笔试算法题(05):转换BST为双向链表 & 查找栈中的最小元素
出题:把二元查找树转变成排序的双向链表.输入一棵二元查找树,要求将该二元查找树按照中序转换成一个排序的双向链表,要求不能创建任何新的节点,只能调整指针的指向: 分析: 递归的思路,当前节点需要进行的处 ...
- php使用命名空间时自动加载机制
命名空间主要为了解决用户编写的代码与PHP内部的类/函数/常量或第三方类/函数/常量之间的名字冲突.不过并不是你定义了使用命名空间的类,就可以在任何地方随意使用了,需要在程序运行时将定义命名空间的类文 ...
- ruby cloud9部署到heroku
Cloud9网址:https://c9.io/ 使用github账号登陆,如果没有,现在github(https://github.com/)上注册一个用户,在进行登陆.
- Android Studio 使用图片
首先将图片放在drawable下 然后: <ImageView android:layout_width="wrap_content" android:layout_heig ...
- Selenium IDE-自动化实战
1.输入测试 Base URL 2.打开录制按钮(默认是录制状态,点击一下就是停止,再次点击,又继续录制) 3.在浏览器界面进行相关操作(比如输入selenium,点击搜索按钮,查看搜索结果),之后点 ...