【题目链接】: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的更多相关文章

  1. CodeForces 527B Error Correct System

    Error Correct System Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  2. 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 ...

  3. Error Correct System(模拟)

     Error Correct System Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  4. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  5. CF Error Correct System

    Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. 【C/C++】Linux下system()函数引发的错误

    http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食  恋恋美食 发布时间: 2012/04/21 11:3 ...

  7. 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 ...

  8. 【63.73%】【codeforces 560A】Currency System in Geraldion

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 22C】 System Administrator

    [题目链接]:http://codeforces.com/problemset/problem/22/C [题意] 给你n个点; 要求你构造一个含m条边的无向图; 使得任意两点之间都联通; 同时,要求 ...

随机推荐

  1. 【POJ 2777】 Count Color

    [题目链接] http://poj.org/problem?id=2777 [算法] 线段树 [代码] #include <algorithm> #include <bitset&g ...

  2. 看看 SDWebImage内部基本实现过程

    一.实现流程 入口 setImageWithURL:placeholderImage:options: 会先把 placeholderImage 显示,然后 SDWebImageManager 根据 ...

  3. activiti安装-------安装插件

    对上面的放大

  4. js mvc框架

    介绍 使用过 JavaScript框架(如 AngularJS, Backbone 或者Ember)的人都很熟悉在UI(用户界面,前端)中mvc的工作机理.这些框架实现了MVC,使得在一个单页面中实现 ...

  5. [Swift通天遁地]四、网络和线程-(10)处理图片:压缩、缩放、圆角、CoreImage滤镜、缓存

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. mysql select 操作优先级

    单表查询操作 select filed1,filed2... form table where ... group by ... having .... order by ... limit ... ...

  7. 看无可看 分治FFT+特征值方程

    题面: 看无可看(see.pas/cpp/c) 题目描述 “What’s left to see when our eyes won’t open?” “若彼此瞑目在即,是否终亦看无可看?” ---- ...

  8. Android内存管理(10)MAT: 基本教程

    原文: http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fgettingstarted%2Fbasic ...

  9. mysql复制数据

    前言:由于工作需要,我要造大量数据,最好的方法是直接copy已有的数据,改其中一些值即可.操作的表有主键,且自增,AUTO_INCREMENT 按照以往的经验无外乎: 类别一. 如果两张张表(导出表和 ...

  10. cocos2d-js 开发常见问题

    1. 编译android版本可能出现的问题记录 如果编译的时候报错.出现/platforms/android-14/arch-arm/usr/lib/crtend_so.o: Unknown EABI ...