Educational Codeforces Round 40 I. Yet Another String Matching Problem
http://codeforces.com/contest/954/problem/I
给你两个串s,p,求上一个串的长度为|p|的所有子串和p的差距是多少,两个串的差距就是每次把一个字符变成另一个字符的最小次数,字符最大到f
很明显,如果知道每两个串对应地方不相同的字符就能通过dfs/dsu解出来,那么如何快速的找到所有对应的不匹配的地方呢,
我们先单独考虑两个不同的字符,比如abada中取a,cba中取c,用二进制1代表选取的字符表示就是10101,100,我们用fft来加速这一过程
1 2 3 4 5 1 2 3 -------> n-1 n-2 n-3
1 0 1 0 1 1 0 0 1 0 0
4 3 1
0 0 1
直接乘的话复杂度还是很高,我们把后一个串转一下变成001,这样如果要找最开始的串和p匹配是不是就系数是5看fft之后的系数是不是1,然后以此类推第二个是6,这样一遍fft就能求出所有情况,然后一共需要fft6*6次,复杂度(O(36*nlogn))
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod (1000000007)
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; struct cd
{
double x,y;
cd(double _x=0.0,double _y=0.0):x(_x),y(_y){}
cd operator + (const cd &b)const
{
return cd(x+b.x,y+b.y);
}
cd operator - (const cd &b)const
{
return cd(x-b.x,y-b.y);
}
cd operator * (const cd &b)const
{
return cd(x*b.x-y*b.y,x*b.y+y*b.x);
}
cd operator / (const double &b)const
{
return cd(x/b,y/b);
}
}a[N<<],b[N<<];
int rev[N<<];
void getrev(int bit)
{
for(int i=; i<(<<bit); i++)
rev[i]=(rev[i>>]>>)|((i&)<<(bit-));
}
void fft(cd* a,int n,int dft)
{
for(int i=; i<n; i++)
if(i<rev[i])
swap(a[i],a[rev[i]]);
for(int step=; step<n; step<<=)
{
cd wn(cos(dft*pi/step),sin(dft*pi/step));
for(int j=; j<n; j+=step<<)
{
cd wnk(,);
for(int k=j; k<j+step; k++)
{
cd x=a[k];
cd y=wnk*a[k+step];
a[k]=x+y;
a[k+step]=x-y;
wnk=wnk*wn;
}
}
}
if(dft==-)for(int i=; i<n; i++)a[i]=a[i]/n;
}
char s[N],p[N];
bool ok[N][][];
int fa[];
int Find(int x)
{
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
int main()
{
scanf("%s%s",s+,p+);
int n=strlen(s+),m=strlen(p+);
int sz=;
while((<<sz)<n)sz++;
sz++;getrev(sz);
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(i==j)continue;
for(int k=;k<=(<<sz);k++)a[k]=b[k]=;
for(int k=;k<=n;k++)a[k]=(s[k]==i+'a');
for(int k=;k<=m;k++)b[n-k]=(p[k]==j+'a');
fft(a,(<<sz),);fft(b,(<<sz),);
for(int k=;k<=(<<sz);k++)a[k]=a[k]*b[k];
fft(a,(<<sz),-);
for(int k=;k<=n-m;k++)ok[k][i][j]=(a[n+k].x>=0.5);
}
}
for(int i=;i<=n-m;i++)
{
for(int j=;j<;j++)fa[j]=j;
int ans=;
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
if(j==k)continue;
int fj=Find(j),fk=Find(k);
if(ok[i][j][k]&&fj!=fk)fa[fj]=fk,ans++;
}
}
printf("%d ",ans);
}
puts("");
return ;
}
/********** **********/
Educational Codeforces Round 40 I. Yet Another String Matching Problem的更多相关文章
- Educational Codeforces Round 40千名记
人生第二场codeforces.然而遇上了Education场这种东西 Educational Codeforces Round 40 下午先在家里睡了波觉,起来离开场还有10分钟. 但是突然想起来还 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- Educational Codeforces Round 40 C. Matrix Walk( 思维)
Educational Codeforces Round 40 (Rated for Div. 2) C. Matrix Walk time limit per test 1 second memor ...
- Educational Codeforces Round 40 (Rated for Div. 2) Solution
从这里开始 小结 题目列表 Problem A Diagonal Walking Problem B String Typing Problem C Matrix Walk Problem D Fig ...
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...
随机推荐
- 模块讲解----shutil模块(copy、压缩、解压)
作用与功能 主要用于文件的copy,压缩,解压 导入shuitl模块: import shutil copy方法 1.shutil.copyfileobj() 打开file1,并copy写入file ...
- oracle的字符集设置与乱码
oracle的字符集设置与乱码 字符集问题一直叫人头疼,究其原因还是不能完全明白其运作原理. 在整个运行环节中,字符集在3个环节中发挥作用: 1.软件在操作系统上运作时的对用户的显示,此时采用操作系统 ...
- 项目实战:Mahout构建图书推荐系统
前言 本文是Mahout实现推荐系统的又一案例,用Mahout构建图书推荐系统.与之前的两篇文章,思路上面类似,侧重点在于图书的属性如何利用.本文的数据在自于Amazon网站,由爬虫抓取获得. 目录 ...
- Java内存解析 程序的执行过程
Java内存解析 栈.堆.常量池等虽同属Java内存分配时操作的区域,但其适用范围和功用却大不相同.本文将深入Java核心,简单讲解Java内存分配方面的知识. 首先我们先来讲解一下内存中的各个区域. ...
- Linux优雅退出问题
问题:Springboot框架开发的项目中会内嵌tomcat容器,在杀死进程的时候tomcat为被正常杀死,导致端口未被释放,第二次启动的时候报端口冲突. 先讲一个基本概念:如何在shell中终止一个 ...
- POJ - 3308 Paratroopers (最小点权覆盖)
题意:N*M个格点,K个位置会有敌人.每行每列都有一门炮,能打掉这一行(列)上所有的敌人.每门炮都有其使用价值.总花费是所有使用炮的权值的乘积.求最小的总花费. 若每门炮的权值都是1,就是求最小点覆盖 ...
- web安全学习方向~两图胜千言~~
- iOS跳转到Touch ID设置界面
1.首先去info.plist 设置: 2.代码 NSURL *url = [NSURL URLWithString:@"App-Prefs:root=TOUCHID_PASSCODE&qu ...
- WCF服务类的实例模式(本文为转载)
WCF开发时如何选择正确的实例模式(InstanceMode)? 在使用WCF实例模型时,你是否思考过这几个的问题: ”WCF中的实例模式如何正确应用”? ”使用WCF中的实例模式有何原则可以遵循 ...
- 20145324 《Java程序设计》第4周学习总结
20145324 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 1.继承是为避免多个类间重复定义共同行为 A extends B A继承B的行为 2.一个子类只能继承一个父 ...