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 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.
看了别人的代码,发现两个字符串的数字匹配只有26*26*2种情况,所以读入所有情况就行了,这里有一点要注意,就是a[i][j]等于i,这样待会要输出交换位置时就可以直接输出了。
#include<stdio.h>
#include<string.h>
char str1[200005],str2[200005];
int a[40][40];
int main()
{
int n,i,t,flag,x,y,k,j;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
memset(str1,0,sizeof(str1));
memset(str2,0,sizeof(str1));
scanf("%s%s",str1+1,str2+1);
t=flag=x=y=0;
for(i=1;i<=n;i++)
{
if(str1[i]!=str2[i])
{
a[str1[i]-'a'+1][str2[i]-'a'+1]=i;
t++;
}
} for(i=1;i<=26;i++)
{
for(j=1;j<=26;j++)
{
if(a[i][j] && a[j][i])
{
flag=1;
x=a[i][j];
y=a[j][i];
break;
}
}
if(flag==1)
break;
}
if(flag==1)
{
printf("%d\n",t-2);
printf("%d %d\n",x,y);
continue;
} for(i=1;i<=26;i++)
{
for(j=1;j<=26;j++)
{
if(a[i][j])
{
for(k=1;k<=26;k++)
{
if(a[j][k])
{
flag=1;
x=a[i][j];
y=a[j][k];
break;
}
}
}
if(flag==1)
break;
}
if(flag==1)
break;
}
if(flag==1)
{
printf("%d\n",t-1);
printf("%d %d\n",x,y);
continue;
} printf("%d\n",t);
printf("-1 -1\n");
}
return 0;
}
Codeforces Round #296 (Div. 2B. Error Correct System的更多相关文章
- 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 ...
- 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 ...
- CodeForces Round #296 Div.2
A. Playing with Paper 如果a是b的整数倍,那么将得到a/b个正方形,否则的话还会另外得到一个(b, a%b)的长方形. 时间复杂度和欧几里得算法一样. #include < ...
- 字符串处理 Codeforces Round #296 (Div. 2) B. Error Correct System
题目传送门 /* 无算法 三种可能:1.交换一对后正好都相同,此时-2 2.上面的情况不可能,交换一对后只有一个相同,此时-1 3.以上都不符合,则不交换,-1 -1 */ #include < ...
- Codeforces Round #313 (Div. 2) A. Currency System in Geraldion
A. Currency System in Geraldion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/co ...
- Codeforces Round #313 (Div. 2) A. Currency System in Geraldion 水题
A. Currency System in Geraldion Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...
- Codeforces Round #296 (Div. 1) E. Triangles 3000
http://codeforces.com/contest/528/problem/E 先来吐槽一下,一直没机会进div 1, 马力不如当年, 这场题目都不是非常难,div 2 四道题都是水题! 题目 ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表
E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...
- 【打CF,学算法——一星级】Codeforces Round #313 (Div. 2) A. Currency System in Geraldion
[CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/A 题面: A. Currency System in Geraldion time l ...
随机推荐
- 计算机考研复试 A+B
题目描述 读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出. 输入描述: 测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B ...
- MySQL常用字符串函数和日期函数
数据函数 SELECT ABS(-8); /*绝对值*/ SELECT CEILING(9.4); /*向上取整*/ SELECT FLOOR(9.4); /*向下取整*/ SELECT RAND() ...
- 【Nginx】配置nginx图片服务器
想通过nginx来访问服务器上的图片 可以搭建一个nginx图片服务器. 做法如下: 先安装nginx,这里直接用yum来进行安装的 安装方法如下: https://blog.csdn.net/iml ...
- 【Linux】ABRT has detected 1 problem(s). For more info run: abrt-cli list --since 1548988705
------------------------------------------------------------------------------------------------- | ...
- EFCore 5 新特性 —— Savepoints
EFCore 5 中的 Savepoints Intro EFCore 5中引入了一个新特性,叫做 Savepoints,主要是事务中使用,个人感觉有点类似于 Windows 上的系统还原点,如果事务 ...
- 阿里云OSS对象存储服务(二)
一.使用SDK 在OSS的概览页右下角找到"Bucket管理",点击"OSS学习路径" 点击"Java SDK"进入SDK开发文档 二.创建 ...
- 第2章_神经网络入门_2-5&2-6 数据处理与模型图构建
目录 神经元的TF实现 安装 神经网络的TF实现 神经元的TF实现 安装 版本: Python 2.7 tf 1.8.0 Linux 略 demo 神经网络的TF实现 # py36 tf 2.1. # ...
- Vue基础之Vue组件
Vue基础之Vue组件 // 组件是可以复用的Vue实例! // 可以把经常重复的功能封装为组件!
- windows IOCP 实践
关于 windows IOCP 有人说 windows IOCP 是 windows 上最好的东西. IOCP 是真正的异步 IO,意味着每次发起一个 IO 请求,该调用本身则立即返回, 而包括 IO ...
- CF1190B
扯在前面 我们老师刚讲过的题目,很考验思维,本蒟蒻WA了十发才过,然后看到题解里只是指出了特殊情况没多解释,可能有人看不懂,特来分享一下 首先题目就很有意思,思考的过程也很有趣,想把所有情况思考全思考 ...