HDU 5763 Another Meaning(FFT)
【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5763
【题目大意】
给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式。
【题解】
我们定义数组f为S串中T出现的最后一个字母所在的位置,那么ans[i]=ans[i-1]+f[i-1]?ans[i-lenT]:0,一遍递推即可,所以关键就在于求出f数组了,f数组可以用kmp求,由于最近练FFT,用FFT求距离卷积匹配为0的位置,就是f数组了。
【代码】
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=524300;
int n,pos[N];
namespace FFT{
struct comp{
double r,i;
comp(double _r=0,double _i=0):r(_r),i(_i){}
comp operator +(const comp&x){return comp(r+x.r,i+x.i);}
comp operator -(const comp&x){return comp(r-x.r,i-x.i);}
comp operator *(const comp&x){return comp(r*x.r-i*x.i,i*x.r+r*x.i);}
comp conj(){return comp(r,-i);}
}A[N],B[N];
const double pi=acos(-1.0);
void FFT(comp a[],int n,int t){
for(int i=1;i<n;i++)if(pos[i]>i)swap(a[i],a[pos[i]]);
for(int d=0;(1<<d)<n;d++){
int m=1<<d,m2=m<<1;
double o=pi*2/m2*t;
comp _w(cos(o),sin(o));
for(int i=0;i<n;i+=m2){
comp w(1,0);
for(int j=0;j<m;j++){
comp& A=a[i+j+m],&B=a[i+j],t=w*A;
A=B-t;B=B+t;w=w*_w;
}
}
}if(t==-1)for(int i=0;i<n;i++)a[i].r/=n;
}
}
const int mod=1e9+7;
int T,Cas=1,l1,l2,ans[N],cnt=0,a[N],b[N],f[N];
FFT::comp A[N],B[N],C[N];
char s1[N],s2[N];
int main(){
scanf("%d",&T);
while(T--){
scanf(" %s %s",&s1,&s2);
memset(f,0,sizeof(f));
memset(ans,0,sizeof(ans));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
l1=strlen(s1); l2=strlen(s2);
for(int i=0;i<l1;i++)a[i]=s1[i]-'a'+1;
for(int i=0;i<l2;i++)b[l2-1-i]=s2[i]-'a'+1;
int N=1; while(N<l1+l2)N<<=1;
int j=__builtin_ctz(N)-1;
for(int i=0;i<N;i++)C[i]=FFT::comp(0,0);
for(int i=0;i<N;i++){pos[i]=pos[i>>1]>>1|((i&1)<<j);}
for(int i=0;i<N;i++)A[i]=FFT::comp(a[i]*a[i]*a[i],0),B[i]=FFT::comp(b[i],0);
FFT::FFT(A,N,1);FFT::FFT(B,N,1);
for(int i=0;i<N;i++)C[i]=C[i]+A[i]*B[i];
for(int i=0;i<N;i++)A[i]=FFT::comp(a[i],0),B[i]=FFT::comp(b[i]*b[i]*b[i],0);
FFT::FFT(A,N,1);FFT::FFT(B,N,1);
for(int i=0;i<N;i++)C[i]=C[i]+A[i]*B[i];
for(int i=0;i<N;i++)A[i]=FFT::comp(a[i]*a[i],0),B[i]=FFT::comp(b[i]*b[i],0);
FFT::FFT(A,N,1);FFT::FFT(B,N,1);
for(int i=0;i<N;i++)C[i]=C[i]-A[i]*B[i]*FFT::comp(2,0);
FFT::FFT(C,N,-1);
for(int i=l2-1;i<l1;i++){
if(C[i].r<0.5)f[i]=1;
}ans[0]=1;
for(int i=1;i<=l1;i++){
ans[i]=ans[i-1];
if(f[i-1])ans[i]+=ans[i-l2];
if(ans[i]>mod)ans[i]-=mod;
}printf("Case #%d: %d\n",Cas++,ans[l1]);
}return 0;
}
HDU 5763 Another Meaning(FFT)的更多相关文章
- HDU 5763 Another Meaning (kmp + dp)
Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...
- HDU 5763 Another Meaning(DP+KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意: 给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思. 思路:因为肯定要去字符串去找模 ...
- A * B Problem Plus HDU - 1402 (FFT)
A * B Problem Plus HDU - 1402 (FFT) Calculate A * B. InputEach line will contain two integers A and ...
- HDU 5763 Another Meaning
HDU 5763 Another Meaning 题意:一个字串有可能在模式串出现多次,问有多少种可能出现的情况.关键是有重合的字串是不能同时计入的. 思路:先用kmp求出所有字串的位置.然后,dp. ...
- 快速傅里叶(FFT)的快速深度思考
关于按时间抽取快速傅里叶(FFT)的快速理论深度思考 对于FFT基本理论参考维基百科或百度百科. 首先谈谈FFT的快速何来?大家都知道FFT是对DFT的改进变换而来,那么它究竟怎样改进,它改进的思想在 ...
- HDU 5938 Four Operations(四则运算)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
随机推荐
- leetcode Binary Tree Right Side View python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- Eclipse 修改字体
- CSS3弹性盒模型布局模块
原文:http://robertnyman.com/2010/12/02/css3-flexible-box-layout-module-aka-flex-box-introduction-and-d ...
- css中em与px
Px是绝对定位 em是相对定位 1. em的值并不是固定的: 2. em会继承父级元素的字体大小. em应用: 1. body选择器中声明Font-size=62.5%:(注:在ie中把body选 ...
- 限定只能处理"A仓"和"B仓"入库
应用 Oracle Inventory 层 Level Function 函数名 Funcgtion Name INV_INVTTMTX_MISC 表单名 Form Name INVTTMTX 说明 ...
- Get Intellisense for .axml files in Visual Studio
原文Get Intellisense for .axml files in Visual Studio So in order to get some intellisense support for ...
- 圣何塞与 Microsoft 宣布该市为超过 5,000 名市府公务员选择 Office 365、Windows Azure 和 StorSimple
过去几个月来我们展示了极大的客户吸引力,今天我们非常高兴地宣布,我们又赢得了一位新客户,且他们利用 Microsoft 革新 IT 的方式非常有趣. 今天,我们非常高兴地告诉大家,圣何塞市选择了 Mi ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
- E=MC2 - 搜搜百科
E=MC2 - 搜搜百科 1 E=MC2 质能等价理论是爱因斯坦狭义相对论的最重要的推论,即著名的方程式E=mC^2,式中E为能量,m为质量,C为光速:也就是说,一切物质都潜藏着质量乘于光速平方的能量 ...
- javascript第二课javascript规范
1.javascript严格区分大小写 2.声明变量一律使用var 推断类型 3.每条语句后面加分号 4.字符串使用单引号 5.html代码使用双引号,js用单引号