954I Yet Another String Matching Problem
分析
我们先考虑暴力如何计算
对于S的子串SS,如果它有位置i使得SS[i] != T[i]那么我们就将两个字符之间用并查集连边
最后答案很明显就是并查集中所有边的个数
于是我们可以发现对于S[i] != T[j]衣服那个会在S的第i-j个子串连边
我们通过观察可以发现i - j = i - (m - j) +m
这是一个卷积的形式
于是我们枚举S和T中考虑的是那种字符然后卷积判断连边关系
最后进行之前说的并查集即可
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int mod = ;
const int g = ;
int fa[][];
char s[],ot[],t[];
int n,m,r[],len,N,a[],b[],G,ans[];
inline int pw(int x,int p){
int res=;
while(p){
if(p&)res=1ll*res*x%mod;
x=1ll*x*x%mod;
p>>=;
}
return res;
}
inline void ntt(int a[],int f){
register int i,j,k;
int now;
for(i=;i<n;++i)
if(i<r[i])swap(a[i],a[r[i]]);
for(i=;i<n;i<<=){
if(f==)now=g;
else now=G;
int wn=pw(now,(mod-)/(i<<));
for(j=;j<n;j+=(i<<)){
int w=,x,y;
for(k=;k<i;++k,w=1ll*w*wn%mod){
x=a[j+k],y=1ll*a[i+j+k]*w%mod;
a[j+k]=(x+y)%mod,a[i+j+k]=(x+mod-y)%mod;
}
}
}
}
inline int sf(int wh,int x){return fa[wh][x]==x?x:fa[wh][x]=sf(wh,fa[wh][x]);}
signed main(){
register int i,j,k;
int on,om;
G=pw(g,mod-);
scanf("%s",s);
scanf("%s",ot);
n=strlen(s),m=strlen(ot);
for(i=;i<m;++i)t[i]=ot[m-i-];
n--,m--;
on=n,om=m;
m+=n;
for(n=;n<=m;n<<=)len++;
for(i=;i<n;++i)r[i]=((r[i>>]>>)|((i&)<<(len-)));
for(i=om;i<=on;++i)
for(j=;j<;++j)fa[i-om][j]=j;
for(i=;i<;++i)
for(j=;j<;++j){
if(i==j)continue;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(k=;k<=on;++k)a[k]=((s[k]-'a')==i);
for(k=;k<=om;++k)b[k]=((t[k]-'a')==j);
ntt(a,),ntt(b,);
for(k=;k<n;k++)a[k]=1ll*a[k]*b[k]%mod;
ntt(a,-);
int inv=pw(n,mod-);
for(k=;k<n;++k)a[k]=1ll*a[k]*inv%mod;
for(k=om;k<=on;++k)if(a[k]){
int x=sf(k-om,i),y=sf(k-om,j);
if(x==y)continue;
ans[k-om]++;
fa[k-om][x]=y;
}
}
for(i=om;i<=on;++i)printf("%d ",ans[i-om]);
return ;
}
954I Yet Another String Matching Problem的更多相关文章
- Codeforces 954I Yet Another String Matching Problem(并查集 + FFT)
题目链接 Educational Codeforces Round 40 Problem I 题意 定义两个长度相等的字符串之间的距离为: 把两个字符串中所有同一种字符变成另外一种,使得两个 ...
- Codeforces.954I.Yet Another String Matching Problem(FFT)
题目链接 \(Description\) 对于两个串\(a,b\),每次你可以选择一种字符,将它在两个串中全部变为另一种字符. 定义\(dis(a,b)\)为使得\(a,b\)相等所需的最小修改次数. ...
- 【CF954I】Yet Another String Matching Problem(FFT)
[CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...
- CF954I Yet Another String Matching Problem 并查集、FFT
传送门 题意:给出两个由小写$a$到$f$组成的字符串$S$和$T$($|S| \geq |T|$),给出变换$c1\,c2$表示将两个字符串中所有$c1$字符变为$c2$,求$S$的每一个长度为$T ...
- Educational Codeforces Round 40 I. Yet Another String Matching Problem
http://codeforces.com/contest/954/problem/I 给你两个串s,p,求上一个串的长度为|p|的所有子串和p的差距是多少,两个串的差距就是每次把一个字符变成另一个字 ...
- CF954I Yet Another String Matching Problem
传送门 每次操作可以把两个字符串中所有同一种字符变成另外一种 定义两个长度相等的字符串之间的距离为:使两个字符串相等所需要操作的次数的最小值 求 \(s\) 中每一个长度为 \(|t|\) 的连续子串 ...
- CF954I Yet Another String Matching Problem(FFT+并查集)
给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)子串与\(T\)的距离 两个等长的串的距离定义为最少的,将某一个字符全部视作另外一个字符的次数. \(|T|<=|S|<= ...
- string matching(拓展KMP)
Problem Description String matching is a common type of problem in computer science. One string matc ...
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
随机推荐
- webpack中imports-loader,exports-loader,expose-loader的区别
Webpack有几个和模块化相关的loader,imports-loader,exports-loader,expose-loader,比较容易混淆.今天,我们来理一理. imports-loader ...
- RTMP(实时信息传输协议)详解
RTMP协议是Real Time Message Protocol(实时信息传输协议)的缩写,它是由Adobe公司提出的一种应用层的协议,用来解决多媒体数据传输流的多路复用(Multiplexing) ...
- Appium ios新的定位方式FindsByIosNSPredicate (没有试 先记录在这里) 有个 driver.find_element_by_ios_uiautomation() 研究下 ios的定位
这个定位方式需要用java-client -5.0.版本,4.x的版本没有这个定位方式 //输入账号和密码 driver.findElementByIosNsPredicate("value ...
- ESXI root密码忘记,重置root密码
今天遇到了一个叫人比较头疼的问题,早在一个月前公司拉来一台服务器,闲着没事我给装成了Esxi的虚拟机系统了,时间过久忘了当时设定的密码为何?故而翻了许久的资料,终于找好的方向,准备重置系统密码.准备搞 ...
- linux 下查找文件或者内容常用命令
转自:http://www.cnblogs.com/sunleecn/archive/2011/11/01/2232210.html whereis <程序名称>查找软件的安装路径-b 只 ...
- 关于模拟admin实现stark组件的知识点
一. url知识 还记得include分发么?里面的参数都可以有些什么? urlconf_module本质是返回的是模块路径对象 def include(arg, namespace=None, ap ...
- css/css3实现未知宽高元素的垂直居中和水平居中
题目:.a{ width: 200px; height: 200px; background-color: #ccc;} <body> <div class="a" ...
- jQuery样式与动画
修改内联CSS .css() 获取 //取得单个属性的值,传入'属性名',返回"value" .css('property') //取得多个属性的值,传入'['属性1','属性2' ...
- Tkinter tkMessageBox
Tkinter tkMessageBox: tkMessageBox模块用于显示在您的应用程序的消息框.此模块提供了一个功能,您可以用它来显示适当的消息 tkMessageBox模块 ...
- 【转】javascript 执行环境,变量对象,作用域链
这篇文章比较清晰的解释了一些作用域链相关的概念,忍不住收藏了 原文地址:http://segmentfault.com/a/1190000000533094 前言 这几天在看<javascrip ...