题目链接:https://codeforces.com/contest/954/problem/I
题目描述:有两个串S,T,你可以执行一种操作:选择两个字符a,b,将两个等长串内的字符a变成字符b。定义使得两个等长的串相同的操作次数为两个串的距离,让你求S中所有子串和T的子串的最短距离。
题解:考虑一般解法,将S的子串和T对应位置的字符连起来,构成一张无向图,这个无向图只有6个点(只有6种字符串),每一个大小为
i
i
i 连通分量最后要变成同一个字符,至少需要
i
−
1
i - 1
i−1次操作,遍历所有的连通分量把答案加起来就可以得到最终答案。
要建边需要遍历两个字符串,FFT可以加速这个过程,类似串的匹配,先将T字符串翻转,然后枚举字符对(a,b),对(a,b)字符对 跑FFT,可以计算出每一个位置开始的子串和T串的字符串对(a,b)的数量,依次来判断是否有(a,b)这条边,然后用并查集维护一下点的个数即可,连通分量的个数可以暴力枚举每个点,因为只有6个点,答案等于 总点数 - 连通分量的个数 。
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 2e5 + 10;
const double pi = acos(-1.0);
char s[maxn],t[maxn];
int ls,lt,len,ans[4 * maxn],sum[4 * maxn];
int p[4 * maxn][8];
int num[4 * maxn][8];
bool vis[8];
int find(int pos,int x) {
return p[pos][x] == x ? x : p[pos][x] = find(pos,p[pos][x]);
}
struct complex{
double r,i;
complex(double rr = 0.0,double ii = 0.0) {
r = rr;
i = ii;
}
complex operator + (const complex & rhs) {
return complex(r + rhs.r,i + rhs.i);
}
complex operator - (const complex & rhs) {
return complex(r - rhs.r,i - rhs.i);
}
complex operator * (const complex & rhs) {
return complex(r * rhs.r - i * rhs.i,r * rhs.i + i * rhs.r);
}
};
complex A[4 * maxn],B[4 * maxn];
void change(complex a[],int len) {
for(int i = 1,j = len / 2; i < len - 1; i++) {
if(i < j) swap(a[i],a[j]);
int k = len / 2;
while(j >= k) {
j -= k;
k /= 2;
}
j += k;
}
}
void fft(complex a[],int len,int type) {
change(a,len);
for(int s = 2; s <= len; s <<= 1) {
complex wp = complex(cos(2 * pi * type / s),sin(2 * pi * type / s));
for(int j = 0; j < len; j += s) {
complex w = complex(1,0);
for(int k = 0; k < s / 2; k++) {
complex t = a[j + k];
complex u = w * a[j + k + s / 2];
a[j + k] = t + u;
a[j + k + s / 2] = t - u;
w = w * wp;
}
}
}
if(type == -1)
for(int i = 0; i < len; i++)
a[i].r /= len;
}
void solve(char a,char b) {
len = 1;
memset(ans,0,sizeof ans);
while(len < 2 * ls || len < 2 * lt) len <<= 1;
for(int i = 0; i < ls; i++)
A[i] = complex(s[i] == a,0);
for(int i = ls; i < len; i++)
A[i] = complex(0,0);
for(int i = 0; i < lt; i++)
B[i] = complex(t[i] == b,0);
for(int i = lt; i < len; i++)
B[i] = complex(0,0);
fft(A,len,1);
fft(B,len,1);
for(int i = 0; i < len; i++)
A[i] = A[i] * B[i];
fft(A,len,-1);
for(int i = 0; i < len; i++) {
int t = (int) (A[i].r + 0.5);
ans[i] += t;
}
int la = a - 'a' + 1,lb = b - 'a' + 1;
for(int i = 0; i < len; i++) {
if(ans[i]) {
if(!p[i][la]) p[i][la] = la,num[i][la] = 1;
if(!p[i][lb]) p[i][lb] = lb,num[i][lb] = 1;
int fla = find(i,p[i][la]),flb = find(i,p[i][lb]);
if(fla != flb) {
num[i][flb] += num[i][fla];
p[i][fla] = flb;
}
}
}
}
int main() {
scanf("%s",s);
scanf("%s",t);
ls = strlen(s);
lt = strlen(t);
reverse(t,t + lt);
for(char i = 'a'; i <= 'f'; i++)
for(char j = 'a'; j <= 'f'; j++) {
solve(i,j);
}
int cnt = 0,tot;
for(int i = lt - 1; i < ls; i++) {
cnt = 0,tot = 0;
memset(vis,0,sizeof vis);
for(char j = 1; j <= 6; j++) {
int f = find(i,p[i][j]);
if(vis[f] || !f) continue;
cnt += num[i][f];
vis[f] = 1;
tot++;
}
if(i - lt + 1) printf(" ");
printf("%d",cnt - tot);
}
return 0;
}