Codeforces 527B

#include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>

using namespace std;

const int maxn = 200005;
char s[maxn];
char t[maxn];
int graph[27][27];
int main(){
    int n;
    scanf("%d", &n);
    scanf("%s", s);
    scanf("%s", t);
    memset(graph, -1, sizeof(graph));
    int match = 0;
    bool exchange = false;
    bool matchone = false;
    int before = -1, after = -1;
    for(int i = 0; i < n; ++i){
        if(s[i] == t[i]){
            ++match;
        }
        else{
            int sidx = s[i] - 'a';
            int tidx = t[i] - 'a';
            graph[sidx][tidx] = i;
        }
    }
    for(int i = 0; i < n; ++i){
        if(s[i] == t[i]){
            continue;
        }
        int sidx = s[i] - 'a';
        int tidx = t[i] - 'a';
        if(graph[tidx][sidx] != -1){
            before = min(graph[sidx][tidx], graph[tidx][sidx]);
            after = max(graph[sidx][tidx], graph[tidx][sidx]);
            exchange = true;
            break;
        }
        for(int j = 0; j < 26; ++j){
            if(graph[tidx][j] != -1){
                before = min(graph[sidx][tidx], graph[tidx][j]);
                after = max(graph[sidx][tidx],graph[tidx][j]);
                matchone = true;
            }
        }
    }
    if(exchange){
        printf("%d\n", n - match - 2);
        printf("%d %d\n", before + 1, after + 1);
    }
    else if(matchone){
        printf("%d\n", n - match - 1);
        printf("%d %d\n", before + 1, after + 1);
    }
    else{
        printf("%d\n", n - match);
        printf("-1 -1\n");
    }
    return 0;
}

之前错误的代码分析:
1. 应该先把字符串扫一遍,在graph数组里标记好,而不是边扫遍看graph,因为在前面遇到不匹配的情况时,后面的字符串还没有扫到,所以相关信息就会丢失
2. 那么为什么不采用双向的扫描方式呢?
因为题目问的是s到t的Ham*距离,而不是 t到s的,所以每次遇到不匹配的情况时,只能看从s的哪个地方能调到这个不匹配的位置,即查看graph[tid][x]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值