#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]