CF954I Yet Another String Matching Problem 并查集、FFT
题意:给出两个由小写$a$到$f$组成的字符串$S$和$T$($|S| \geq |T|$),给出变换$c1\,c2$表示将两个字符串中所有$c1$字符变为$c2$,求$S$的每一个长度为$T$的子串与$T$做变换使得两个字符串相等的最小变换次数。$1 \leq |T| \leq |S| \leq 1.25 \times 10^5$
弱化版:CF939D
PS:默认字符串开头是第$0$位
我们同样考虑通过CF939D的那种方法解决这个问题。考虑到这道题的字符集大小只有$6$,也就是说本质不同的边的条数只有$30$条。我们可以考虑枚举$S$中的字符$x$与$T$中的字符$y$的连边情况。将$T$反序后,将$S$中的字符$x$对应为$1$,T中的字符$y$也对应为$1$,其他的都对应为$0$。然后对这两个对应的数组做$FFT$,这样得到的结果的第$x$位如果不为$0$,意味着$S$的以第$x - |T| + 1$位为开头的子串中存在$x$到$y$的连边(如果不是很理解可以自己画图qwq)。然后对每一个$S$的子串开并查集维护就可以了。复杂度$O(30nlogn)$
#include<bits/stdc++.h>
#define eps 1e-2
#define ld long double
//This code is written by Itst
using namespace std;
inline int read(){
;
;
char c = getchar();
while(c != EOF && !isdigit(c)){
if(c == '-')
f = ;
c = getchar();
}
while(c != EOF && isdigit(c)){
a = (a << ) + (a << ) + (c ^ ');
c = getchar();
}
return f ? -a : a;
}
;
char s1[MAXN] , s2[MAXN];
struct comp{
ld x , y;
comp(ld _x = , ld _y = ){
x = _x;
y = _y;
}
comp operator +(comp a){
return comp(x + a.x , y + a.y);
}
comp operator -(comp a){
return comp(x - a.x , y - a.y);
}
comp operator *(comp a){
return comp(x * a.x - y * a.y , x * a.y + y * a.x);
}
}A[MAXN] , B[MAXN];
);
] , ans[MAXN] , dir[MAXN] , need;
inline void FFT(comp* a , int type){
; i < need ; ++i)
if(i < dir[i])
swap(a[i] , a[dir[i]]);
; i < need ; i <<= ){
comp wn(cos(pi / i) , type * sin(pi / i));
; j < need ; j += i << ){
comp w( , );
; k < i ; ++k , w = w * wn){
comp x = a[j + k] , y = a[i + j + k] * w;
a[j + k] = x + y;
a[i + j + k] = x - y;
}
}
}
}
bool cmp(ld a , ld b){
return a - eps < b && a + eps > b;
}
int find(int dir , int x){
return fa[dir][x] == x ? x : (fa[dir][x] = find(dir , fa[dir][x]));
}
int main(){
#ifndef ONLINE_JUDGE
freopen("954I.in" , "r" , stdin);
//freopen("954I.out" , "w" , stdout);
#endif
scanf("%s%s" , s1 , s2);
int l1 = strlen(s1) , l2 = strlen(s2);
; i < (l2 >> ) ; ++i)
swap(s2[i] , s2[l2 - i - ]);
need = ;
)
need <<= ;
; i <= l1 - l2 ; ++i)
; j <= ; ++j)
fa[i][j] = j;
; i < need ; ++i)
dir[i] = (dir[i >> ] >> ) | (i & ? need >> : );
; i <= ; ++i)
; j <= ; ++j)
if(i != j){
; k < need ; ++k){
A[k].x = s1[k] == 'a' + i;
A[k].y = ;
}
; k < need ; ++k){
B[k].x = s2[k] == 'a' + j;
B[k].y = ;
}
FFT(A , );
FFT(B , );
; k < need ; ++k)
A[k] = A[k] * B[k];
FFT(A , -);
; k < l1 ; ++k)
))
, i + ) != find(k - l2 + , j + )){
fa[k - l2 + ][find(k - l2 + , i + )] = find(k - l2 + , j + );
++ans[k - l2 + ];
}
}
; i <= l1 - l2 ; ++i)
printf("%d " , ans[i]);
;
}
CF954I Yet Another String Matching Problem 并查集、FFT的更多相关文章
- CF954I Yet Another String Matching Problem(FFT+并查集)
给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)子串与\(T\)的距离 两个等长的串的距离定义为最少的,将某一个字符全部视作另外一个字符的次数. \(|T|<=|S|<= ...
- CF954I Yet Another String Matching Problem
传送门 每次操作可以把两个字符串中所有同一种字符变成另外一种 定义两个长度相等的字符串之间的距离为:使两个字符串相等所需要操作的次数的最小值 求 \(s\) 中每一个长度为 \(|t|\) 的连续子串 ...
- 【CF954I】Yet Another String Matching Problem(FFT)
[CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...
- Codeforces 954I Yet Another String Matching Problem(并查集 + FFT)
题目链接 Educational Codeforces Round 40 Problem I 题意 定义两个长度相等的字符串之间的距离为: 把两个字符串中所有同一种字符变成另外一种,使得两个 ...
- 954I Yet Another String Matching Problem
传送门 分析 我们先考虑暴力如何计算 对于S的子串SS,如果它有位置i使得SS[i] != T[i]那么我们就将两个字符之间用并查集连边 最后答案很明显就是并查集中所有边的个数 于是我们可以发现对于S ...
- Codeforces.954I.Yet Another String Matching Problem(FFT)
题目链接 \(Description\) 对于两个串\(a,b\),每次你可以选择一种字符,将它在两个串中全部变为另一种字符. 定义\(dis(a,b)\)为使得\(a,b\)相等所需的最小修改次数. ...
- A simple problem(并查集判环)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2497 题意:给定一些点和边的关系,判断S点是否 ...
- String Reconstruction (并查集)
并查集维护和我这个位置的字母连续的已经被填充的字母能到达的最右边的第一个还没有填充的位置,然后把这个位置填上应该填的东西,然后把这个位置和下一个位置连接起来,如果下一个位置还没有填,我就会把下一个位置 ...
- CodeForces 828C String Reconstruction(并查集思想)
题意:给你n个串,给你每个串在总串中开始的每个位置,问你最小字典序总串. 思路:显然这道题有很多重复填涂的地方,那么这里的时间花费就会特别高. 我们维护一个并查集fa,用fa[i]记录从第i位置开始第 ...
随机推荐
- php向数据库插入数据
<?php header("Content-type: text/html;charset=utf-8"); $con = mysql_connect("local ...
- linux Mate Lxde 消耗资源对比
腾讯云 标准型SA1 | SA1.SMALL1 1核1G AMD LXDE: MATE:
- Spring Boot Actuator认识
概述 spring-boot-starter-actuator:是一个用于暴露自身信息的模块,主要用于监控与管理. 为了保证actuator暴露的监控接口的安全性,需要添加安全控制的依赖spring- ...
- LeetCode题解之Balanced Binary Tree
1.题目描述 2.问题分析 DFS. 3.代码 bool isBalanced(TreeNode* root) { if (root == NULL) return true; && ...
- 洗礼灵魂,修炼python(34)--面向对象编程(4)—继承
前面已经说到面向对象编程有封装,继承,多态三大特性,那么其中的继承则很重要,可以直接单独的拿出来解析 继承 1.什么是继承: 字面意是子女继承父母的家产或者特性等.而在编程里继承是指子类继承父类(基类 ...
- Business talking in English
Talking one: A: Microsoft, this is Steve. B: Hi Steve, this is Richard from Third Hand Testing. I am ...
- Can't debug c++ project because unable to static library start program *.lib
Can't debug c++ project because unable to static library start program *.lib I'm using a library ( ...
- Git永久删除文件和历史记录
目录 Git永久删除文件和历史记录 使用filter-branch 添加到.gitignore文件里并push修改后的repo 清理和回收空间 Git永久删除文件和历史记录 造成你想从git存储库中永 ...
- HashMap探究
HashMap 前置 //初始化容量 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; //容器最大容量 static final i ...
- java连接zookeeper服务器出现“KeeperErrorCode = ConnectionLoss for ...”
错误信息如下: Exception in thread "main" org.apache.zookeeper.KeeperException$ConnectionLossExce ...