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 ...
随机推荐
- 关于软件架构中的b/s
**B/S架构 b/s只需要一个浏览器,用户就可以通过不同的网址访问不同的服务器程序. 优点:开发,安装,部署,维护简单 缺点:对硬件要求过高,用户的体验会受到影响 首先是资源分类:**可以分为静态资 ...
- Java Mybatis快速入门之基本使用
目录 搭建环境 编写 Mybatis 核心配置文件 pom导出资源失败 测试 搭建环境 新建Maven项目 导入Maven依赖 <dependencies> <!--mysql驱动- ...
- mysql过滤复制
- 安装sendmail
yum install -y sendmail yum install -y sendmail-cf 启动 service sendmail start 发送邮件 cat nihao.txt |mai ...
- Poj-P3468题解【线段树】
本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=3468 题目描述: 给N个数A1, A2 ...
- [Usaco2008 Open]Roads Around The Farm分岔路口
题目描述 约翰的N(1≤N≤1,000,000,000)只奶牛要出发去探索牧场四周的土地.她们将沿着一条路走,一直走到三岔路口(可以认为所有的路口都是这样的).这时候,这一群奶牛可能会分成两群,分别沿 ...
- Java并发包源码学习系列:阻塞队列实现之ArrayBlockingQueue源码解析
目录 ArrayBlockingQueue概述 类图结构及重要字段 构造器 出队和入队操作 入队enqueue 出队dequeue 阻塞式操作 E take() 阻塞式获取 void put(E e) ...
- :setting:`task_soft_time_limit` celery 异步任务 执行时间限制 内存限制
https://docs.celeryproject.org/en/stable/userguide/configuration.html?highlight=control_exchange#new ...
- TCMalloc源码学习(一)
打算一边学习tcmalloc的源码一边写总结文章.先从转述TCMalloc的一篇官方文档开始(TCMalloc : Thread-Caching Malloc). 为什么用TCMalloc TCMal ...
- Asp.netCore 3.1控制器属性注入and异步事务Aop by AutoFac
Aspect Oriented Programming(AOP)是较为热门的一个话题.AOP,国内我们都习惯称之为:面向切面编程 下面直接code 干货展示:(一般人我还不告诉,嘻嘻) 1:导入相关的 ...