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 ...
 
随机推荐
- Way to MongoDB
			
1.MongoDB术语/概念:database,collection,document,field,indexSQL术语/概念:database,table,row,column,index 2.所有 ...
 - php集成财付通支付接口
			
<?phpif(!defined('DEDEINC')) exit('Request Error!');/** *财付通接口类 */class tenpay{ var $dsql; var $m ...
 - 微信小程序组件navigator
			
导航navigator:官方文档 Demo Code: // redirect.js navigator.js Page({ onLoad: function(options) { this.setD ...
 - [Windows Powershell]-学习笔记(5)
			
Powershell自动化变量 Powershell 自动化变量 是那些一旦打开Powershell就会自动加载的变量,或者说是运行是变量. 这些变量一般存放的内容包括 用户信息:例如用户的根目录$h ...
 - java中的重量级与轻量级概念
			
首先轻量级与重量级是一个相对的概念,主要是对应用框架使用方便性和所提供服务特性等方面做比较的. 比方说EJB就是一个重量级的框架,因为它对所编写的代码有限制,同时它也提供分布式等复杂的功能. 相比之下 ...
 - c++之旅:操作符重载
			
操作符重载 操作符重载可以为操作符添加更多的含义,操作符重载的作用的对象是类 那些操作符可以重载 除了下面几个操作符不能重载外,其它的操作符都能重载 . :: .* ?: sizeof 操作符重载的本 ...
 - win7系统下查看端口的占用情况以及如何删除端口进程
			
经常在本地测试开发使用tomcat的时候容易报端口占用的情况,比如我要查看8080端口的使用情况 1.按如下操作,输入 cmd 回车 2.在doc窗口中输入命令 netstat -ano | f ...
 - PHP 日期格式中 Y与y
			
大写Y效果: $nowtime = time(); $nowtime=date("Y-m-d",$nowtime); 结果: 2015-10-24 小写y效果: $nowtime ...
 - 应用libjpeg提取jpeg质量因子
			
http://blog.csdn.net/lzhq28/article/details/7775222 版权声明:本文为博主原创文章,未经博主允许不得转载. data = new BYTE [cinf ...
 - Python for循环文件
			
for 循环遍历文件:打印文件的每一行 #!/usr/bin/env python fd = open('/tmp/hello.txt') for line in fd: print line, 注意 ...