Codeforces 954I Yet Another String Matching Problem(并查集 + FFT)
题目链接 Educational Codeforces Round 40 Problem I
题意 定义两个长度相等的字符串之间的距离为:
把两个字符串中所有同一种字符变成另外一种,使得两个字符串相等所需要操作的次数的最小值。
求$s$中每一个长度为$t$的长度的连续子串与$t$的距离。字符集为小写字母$a$到$f$
首先解决求两个长度相等的字符串之间的距离这个问题。
$s$和$t$相同位上的字母连一条无向边,最后的答案是$s$和$t$中所有出现过的字符的个数减去这个无向图的连通块个数。
现在考虑$s$的所有子串和$t$匹配的问题。
令$s$的长度为$n$,$t$的长度为$m$
那么s的符合题意的子串一共有$n - m + 1$个。
把这$n - m + 1$个子串看成$n - m + 1$个独立的无向图,每个无向图是独立的;
现在我们两两枚举边(一共$30$条边),我们要做的就是快速求出这$n - m + 1$个无向图中,
有哪些是有这条边的。
这个时候我们把s和t转成一个$01$序列,设为$a$和$b$
在$a$和$b$中,若$a_{i} = b_{j} = 1$, 那么$c_{i-j} = 1$。
我们可以把$b$数组反转之后用FFT加速求出$c$。
那么在那些值为$1$的下标对应的无向图中就一定有这条边,并查集处理一下就好了。
时间复杂度$O(30nlogn)$
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define fi first
#define se second const double PI = acos(-1.0);
const int N = 125010; struct dsu{
int father[10];
void init(){
rep(i, 0, 7) father[i] = i;
} int getfather(int x){
return father[x] == x ? x : father[x] = getfather(father[x]);
} } c[N]; char s[N], t[N];
int a[N << 1], b[N << 1];
int n, m;
int ans[N]; struct Complex{
double x, y;
Complex(double x = 0.0, double y = 0.0) : x(x), y(y){}
Complex operator + (const Complex &b) const{
return Complex(x + b.x, y + b.y);
}
Complex operator - (const Complex &b) const{
return Complex(x - b.x, y - b.y);
}
Complex operator * (const Complex &b) const{
return Complex(x * b.x - y * b.y, x * b.y + y * b.x);
}
}; Complex x1[N << 2], x2[N << 2]; void change(Complex y[], int len){
for (int i = 1, j = len / 2; i < len - 1; i++){
if (i < j) swap(y[i], y[j]);
int k = len / 2;
while (j >= k){
j -= k;
k /= 2;
}
if (j < k) j += k;
}
} void fft(Complex y[], int len, int on){
change(y, len);
for (int h = 2; h <= len; h <<= 1){
Complex wn(cos(-on * 2 * PI / h), sin(-on * 2 * PI / h));
for (int j = 0; j < len; j += h){
Complex w(1, 0);
for (int k = j; k < j + h / 2; k++){
Complex u = y[k];
Complex t = w * y[k + h / 2];
y[k] = u + t;
y[k + h / 2] = u - t;
w = w * wn;
}
}
} if (on == -1){
rep(i, 0, len - 1) y[i].x /= len;
}
} void mul(int p[], int dp, int q[], int dq){
int len = 1;
while (len <= dp + dq) len <<= 1; rep(i, 0, dp)
x1[i] = Complex(p[i], 0); rep(i, dp + 1, len - 1)
x1[i] = Complex(0, 0); rep(i, 0, dq)
x2[i] = Complex(q[i], 0); rep(i, dq + 1, len - 1)
x2[i] = Complex(0, 0); fft(x1, len, 1);
fft(x2, len, 1); rep(i, 0, len - 1)
x1[i] = x1[i] * x2[i]; fft(x1, len, -1); rep(i, 0, dp + dq)
p[i] = (int)(x1[i].x + 0.5); rep(i, 0, dp + dq)
if (p[i] > 0) p[i] = 1; dp += dq;
} void work(int pos, int x, int y){
int fx = c[pos].getfather(x), fy = c[pos].getfather(y);
if (fx == fy) return;
assert(pos >= 1 && pos <= n - m + 1);
++ans[pos];
c[pos].father[fx] = fy;
} int main(){ scanf("%s%s", s, t); n = strlen(s), m = strlen(t);
rep(i, 1, n - m + 1) c[i].init(); rep(i, 0, 5){
rep(j, 0, 5){
if (i == j) continue;
memset(a, 0, sizeof a);
memset(b, 0, sizeof b); rep(k, 0, n - 1) a[k] = (s[k] - 'a' == i);
rep(k, 0, m - 1) b[k] = (t[k] - 'a' == j); reverse(b, b + m);
mul(a, n, b, m);
for (int k = m - 1, cnt = 1; cnt <= n - m + 1; ++k, ++cnt){
if (a[k]){
work(cnt, i + 1, j + 1);
}
}
}
} rep(i, 1, n - m + 1) printf("%d\n", ans[i]);
return 0; }
Codeforces 954I Yet Another String Matching Problem(并查集 + FFT)的更多相关文章
- CF954I Yet Another String Matching Problem 并查集、FFT
传送门 题意:给出两个由小写$a$到$f$组成的字符串$S$和$T$($|S| \geq |T|$),给出变换$c1\,c2$表示将两个字符串中所有$c1$字符变为$c2$,求$S$的每一个长度为$T ...
- Codeforces.954I.Yet Another String Matching Problem(FFT)
题目链接 \(Description\) 对于两个串\(a,b\),每次你可以选择一种字符,将它在两个串中全部变为另一种字符. 定义\(dis(a,b)\)为使得\(a,b\)相等所需的最小修改次数. ...
- 954I Yet Another String Matching Problem
传送门 分析 我们先考虑暴力如何计算 对于S的子串SS,如果它有位置i使得SS[i] != T[i]那么我们就将两个字符之间用并查集连边 最后答案很明显就是并查集中所有边的个数 于是我们可以发现对于S ...
- 【CF954I】Yet Another String Matching Problem(FFT)
[CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- CodeForces 828C String Reconstruction(并查集思想)
题意:给你n个串,给你每个串在总串中开始的每个位置,问你最小字典序总串. 思路:显然这道题有很多重复填涂的地方,那么这里的时间花费就会特别高. 我们维护一个并查集fa,用fa[i]记录从第i位置开始第 ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces 755C:PolandBall and Forest(并查集)
http://codeforces.com/problemset/problem/755/C 题意:该图是类似于树,给出n个点,接下来p[i]表示在树上离 i 距离最远的 id 是p[i],如果距离相 ...
- Codeforces Beta Round #5 E. Bindian Signalizing 并查集
E. Bindian Signalizing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
随机推荐
- 《Cracking the Coding Interview》——第2章:链表——题目4
2014-03-18 02:27 题目:将一个单链表按照一个值X分为两部分,小于X的部分放在大于等于X的部分之前. 解法:按照值和X的大小,分链表为两条链表,然后连起来成一条. 代码: // 2.4 ...
- ssm项目中ueditor富文本编辑器的使用
一.下载 https://ueditor.baidu.com/website/index.html 将ueditor放到项目中合适的位置 二 . 配置文件上传路径 在utf8-jsp/jsp/conf ...
- ASP.Net MVC+EF架构
ASP.Net MVC是UI层的框架,EF是数据访问的逻辑. 如果在Controller中using DbContext,把查询的结果的对象放到cshtml中显示,那么一旦在cshtml中访问关联属性 ...
- Linux cooked-mode capture 格式转换
tcpdump抓包时,如果-i选项指定为一个网卡地址,那么抓取的数据包数据链路层是以太网头部:如果指定any,则以太网头部将被替换为linux cooked capture头部 # tcpdump - ...
- HDU 2295 Radar (二分 + Dancing Links 重复覆盖模型 )
以下转自 这里 : 最小支配集问题:二分枚举最小距离,判断可行性.可行性即重复覆盖模型,DLX解之. A*的启发函数: 对当前矩阵来说,选择一个未被控制的列,很明显该列最少需要1个行来控制,所以ans ...
- android之SlideMenu双向滑动
开始动手之前先来讲一下实现原理,在一个Activity的布局中需要有三部分,一个是左侧菜单的布局,一个是右侧菜单的布局,一个是内容布局.左侧菜单居屏幕左边缘对齐,右侧菜单居屏幕右边缘对齐,然后内容布局 ...
- vue components & `@import css` bug
vue components @import css not support css3 @import https://github.com/vuejs/vue-loader/issues/138#i ...
- Redux & React & react-redux
Redux Redux & React & react-redux https://redux.js.org/ https://redux.js.org/api https://red ...
- Spring 对属性文件的加密与解密
一般用于配置密码等敏感信息 解密/加密工具类 package com.baobaotao.placeholder; import sun.misc.BASE64Decoder; import sun. ...
- Android中如何为自定义控件增加状态?
在android开发中我们常常需要对控件进行相关操作,虽然网上已有很多对控件酷炫的操作,但小编今天给大家分享的纯属实用出发.在查看了一些列安卓教程和文档后,发现了一位大牛分享的非常不错的有关andro ...