【codeforces 527B】Error Correct System
【题目链接】:http://codeforces.com/contest/527/problem/B
【题意】
给你两个字符串;
允许你交换一个字符串的两个字符一次;
问你这两个字符串最少会有多少个位置上的字符不同
【题解】
考虑一次交换能够造成的结果
->不同的字符个数减少1
->不同的字符个数减少2
先考虑减少1
记录第一个字符串哪些字符和第二个字符串的不一样记录下来
然后扫描第二个字符串的每个字符,找到不同字符的位置,看看第二个字符串那个位置的字符有没有在第一个字符串里面出现过(刚才有记录的);
如果有的话,就说明能找到不同字符个数减少1的情况;
然后再考虑减少2的情况;
直接扫描一遍字符
如果s1[i]!=s2[i];
则a[s1[i]][s2[i]]=i
然后两层for循环i,j
如果a[i][j]&&a[j][i];
则交换a[i][j]和a[j][i]的字符,就能减少2了;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 256;
const int L = 2e5 + 100;
int a[256][256],n,v[256],l=-1,r=-1,sub,ans = 0;
char s[L], t[L];
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n);
scanf("%s", s + 1);
scanf("%s", t + 1);
rep1(i, 1, n)
if (s[i]!=t[i])
{
ans++;
v[s[i]] = i;
a[s[i]][t[i]] = i;
}
rep1(i,1,n)
if (t[i] != s[i])
{
if (v[t[i]])
{
l = v[t[i]], r = i;
sub = 1;
break;
}
}
for (char i = 'a';i <= 'z';i++)
for (char j = 'a';j <='z';j++)
if (a[i][j] && a[j][i])
{
l = a[i][j], r = a[j][i];
sub = 2;
break;
}
ans -= sub;
printf("%d\n", ans);
printf("%d %d\n", l, r);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 527B】Error Correct System的更多相关文章
- CodeForces 527B Error Correct System
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- 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(模拟)
Error Correct System Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- CF Error Correct System
Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 【C/C++】Linux下system()函数引发的错误
http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食 恋恋美食 发布时间: 2012/04/21 11:3 ...
- 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 ...
- 【63.73%】【codeforces 560A】Currency System in Geraldion
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 22C】 System Administrator
[题目链接]:http://codeforces.com/problemset/problem/22/C [题意] 给你n个点; 要求你构造一个含m条边的无向图; 使得任意两点之间都联通; 同时,要求 ...
随机推荐
- What is an ISAPI Extension?
https://www.codeproject.com/Articles/1432/What-is-an-ISAPI-Extension Introduction Unless you have be ...
- Javascript中的Object对象
Object是在javascript中一个被我们经常使用的类型,而且JS中的所有对象都是继承自Object对象的.虽说我们平时只是简单地使用了Object对象来存储数据,并没有使用到太多其他功能,但是 ...
- createrepo -g /enp/comps.xml .
cd /enp; createrepo -g /enp/comps.xml .
- sizeof中的表达式不执行
char a = 255; printf("%d\n",a); printf("%d\n",sizeof(++a)); printf( ...
- Django day26 HyperlinkedIdentityField,序列化组件的数据校验以及功能的(全局,局部)钩子函数,序列化组件的反序列化和保存
一:HyperlinkedIdentityField(用的很少):传三个参数:第一个路由名字,用来反向解析,第二个参数是要反向解析的参数值,第三个参数:有名分组的名字 -1 publish = ser ...
- $P5018 对称二叉树$
problem 一直忘记给这个题写题解了. 这题挺水的吧. 挺后悔当时没写出来. #ifdef Dubug #endif #include <bits/stdc++.h> using na ...
- [转]Linux命令之iconv
转自:http://lorna8023.blog.51cto.com/777608/420313 用途说明 iconv命令是用来转换文件的编码方式的(Convert encoding of given ...
- 6.11---上传图片遇到的bug,字节流输入流输出流----图解----图片必须是post
!!!这里要注意不能是目录必须是指定的文件名+目录,不然就存照片到指定的目录不成功 ----------------------------------------完整controller-servi ...
- jQuery中关于如何使用animate自定义动画
动画 animate() 01.animate()方法的简单使用 有些复杂的动画通过之前学到的几个动画函数是不能够实现,这时候就是强大的animate方法了. 操作一个元素执行3秒的淡入动画,对比下一 ...
- CF831C Jury Marks
思路: 关键在于“插入”一个得分之后,其他所有得分也随之确定了. 实现: #include <iostream> #include <cstdio> #include < ...