【题目链接】: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. 学习一点汇编 INT 16H指令

    转自:http://blog.pfan.cn/feling/16292.html 功能号:00H和10H 功能:从键盘读入字符 入口参数:AH          =00H—读键盘           ...

  2. dB/oct 解释

    分频斜率(也称滤波器的衰减斜率)用来反映分频点以下频响曲线的下降斜率,用分贝/倍频程(dB/oct)来表示.它有一阶(6 dB/oct).二阶(12 dB/oct).三阶(18 dB/oct)和四阶( ...

  3. MSP430:PWM产生

    #define     PWM                      BIT6 //  Description: This program generates one PWM output on ...

  4. oracle查询时间段

    select count(*) tt from crm_cisco_call_detailwhere DateTime between to_date('2016-4-5 00:00:00','yyy ...

  5. Linux文件属性相关补充及软硬连接

    第1章 文件属性相关 1.1 文件的属性 1.1.1 扩展名 windows  通过扩展名区分不同的类型的文件 linux 扩展名是给人类看的 方便我们区分不同类型文件 .conf      配置文件 ...

  6. php实现下载

    PHP实现下载文件的两种方法.分享下,有用到的朋友看看哦. 方法一: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php /** * 下载文件 * ...

  7. Java使用Player播放mp3

    大家平时闲了都会听听歌,散散心,于是很多人就问,在Java里边如何播放歌曲呢,唉,别说,在Java里边还真能歌曲,下面我为大家揭晓. 我们都知道Java里边做什么都需要对应的jar包,首先贴上mave ...

  8. mysql多表查询 查询排序

    有 ask 问题表  和 answer回答表  回答表中的ask_id和 ask表中的id对应 1.查询 /*查询回答了的 */select a.id,a.title,count(b.ask_id) ...

  9. Android内存管理(9)*MAT:Heap Dump,Shallow Heap,Retained Heap,Dominating Tree,GC Roots等的含义

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

  10. [转]linux sudo 命令

    转自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/11/11/2245341.html “Sudo”是Unix/Linux平台上的一个 ...