Puzzle Lover

CodeForces - 613E

Oleg Petrov loves crossword puzzles and every Thursday he buys his favorite magazine with crosswords and other word puzzles. In the last magazine Oleg found a curious puzzle, and the magazine promised a valuable prize for it's solution. We give a formal description of the problem below.

The puzzle field consists of two rows, each row contains n cells. Each cell contains exactly one small English letter. You also are given a word w, which consists of ksmall English letters. A solution of the puzzle is a sequence of field cells c1, ..., ck, such that:

  • For all i from 1 to k the letter written in the cell ci matches the letter wi;
  • All the cells in the sequence are pairwise distinct;
  • For all i from 1 to k - 1 cells ci and ci + 1 have a common side.

Oleg Petrov quickly found a solution for the puzzle. Now he wonders, how many distinct solutions are there for this puzzle. Oleg Petrov doesn't like too large numbers, so calculate the answer modulo 109 + 7.

Two solutions ci and c'i are considered distinct if the sequences of cells do not match in at least one position, that is there is such j in range from 1 to k, such that cj ≠ c'j.

Input

The first two lines contain the state of the field for the puzzle. Each of these non-empty lines contains exactly n small English letters.

The next line is left empty.

The next line is non-empty and contains word w, consisting of small English letters.

The length of each line doesn't exceed 2 000.

Output

Print a single integer — the number of distinct solutions for the puzzle modulo 109 + 7.

Examples

Input
code
edoc code
Output
4
Input
aaa
aaa aa
Output
14

sol:超好的一道dp题
事实上我代码和上面略有不同
提醒一下:注意一点就是不要算重,比如要向左或向右绕一圈回来的时候一圈的长度最小值为4,因为直接往下走的话在中间那段的转移中已经算过了
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n') const int N=;
const ll Base=,Mod=;
int n,Len;
char S[][N],T[N];
inline void Ad(ll &x,ll y)
{
x+=y; x-=(x>=Mod)?Mod:;
}
ll Seed[N];
inline void Prepare(int n)
{
int i; Seed[]=;
for(i=;i<=n;i++) Seed[i]=1ll*Seed[i-]*Base%Mod;
}
struct Hash
{
ll Hash[N];
inline void Make(int n,char *S)
{
int i; Hash[]=; for(i=;i<=n;i++) Hash[i]=(Hash[i-]*Base%Mod+(S[i]-'a'+))%Mod;
}
inline ll Ask(int l,int r)
{
return (ll)(Hash[r]+Mod-Hash[l-]*Seed[r-l+]%Mod)%Mod;
}
}Pre[],Suf[],HT;
ll dp[][N][N];
//dp[i][j][k]表示在第i行,第j-1个(即i,j在当前点右边),已经匹配了k为的方案数
inline ll Solve(bool opt)
{
ll res=;
int i,j,k;
memset(dp,,sizeof dp);
for(j=;j<=n;j++)
{
dp[][j][]=dp[][j][]=;
for(i=;i<=;i++) for(k=;k<=min(n-j+,Len/);k++) //向右绕一圈回来
{
if(Pre[i].Ask(j,j+k-)==HT.Ask(Len-*k+,Len-k)&&Suf[i^].Ask(n-(j+k-)+,n-j+)==HT.Ask(Len-k+,Len))
{
if((k*!=Len)||opt) Ad(res,dp[i][j][Len-k*]);
}
}
for(i=;i<=;i++) for(k=;k<=min(j,Len/);k++) //向左绕一圈回来
{
if(Suf[i].Ask(n-j+,n-(j-k+)+)==HT.Ask(,k)&&Pre[i^].Ask(j-k+,j)==HT.Ask(k+,k*))
{
if((k*!=Len)||opt) Ad(dp[i^][j+][k*],);
}
}
for(i=;i<=;i++) for(k=;k<=Len;k++)
{
if(T[k]==S[i][j])
{
Ad(dp[i][j+][k],dp[i][j][k-]);
if(k<Len&&T[k+]==S[i^][j])
{
Ad(dp[i^][j+][k+],dp[i][j][k-]);
}
}
}
for(i=;i<=;i++) Ad(res,dp[i][j+][Len]);
}
return res;
}
int main()
{
ll i,j,ans=;
Prepare();
scanf("%s",S[]+); n=strlen(S[]+); scanf("%s",S[]+); scanf("%s",T+); Len=strlen(T+);
for(i=;i<=;i++)
{
Pre[i].Make(n,S[i]); reverse(S[i]+,S[i]+n+); Suf[i].Make(n,S[i]); reverse(S[i]+,S[i]+n+);
}
HT.Make(Len,T);
Ad(ans,Solve());
if(Len>)
{
reverse(T+,T+Len+); HT.Make(Len,T); Ad(ans,Solve());
}
if(Len==)
{
for(j=;j<=n;j++) for(i=;i<=;i++) if(S[i][j]==T[]&&S[i^][j]==T[]) Ad(ans,Mod-);
}
Wl(ans);
return ;
}
/*
input
code
edoc
code
output
4 input
aaa
aaa
aa
output
14 input
v
s
sv
output
1
*/

 

最新文章

  1. 【原】SDWebImage源码阅读(二)
  2. Tarjan三把刀
  3. Java集合中Comparator和Comparable接口的使用
  4. c++ 常数后缀说明
  5. JavaScript中的Array
  6. 判断webkit中的js引擎是否是v8
  7. R语言中的箱图介绍 boxplot
  8. (转) 学习C++ -&gt; 向量(vector)
  9. Java程序占用的内存可能会大于Xmx
  10. 如何将Provisioning Profile安装到开发的Mac系统上
  11. 2. 引入springmvc
  12. 为什么局部变量必须以final修饰(或者有final实效:java8)才可以在内部类中使用?
  13. Linux kernel 之 socket 创建过程分析
  14. PAT 1012 数字分类
  15. 12C -- ORA-01017
  16. execl 导出
  17. Go工具和调试详解
  18. Python Web学习笔记之为什么设计GIL
  19. 查询MySQL某字段相同值得重复数据
  20. shared_ptr 的循环依赖问题

热门文章

  1. Hyperledger Fabric-sdk-java
  2. 解决FileInputStream 读取文件中文乱码问题(转)
  3. 如何配置数据库镜像&lt;一&gt;
  4. 转:Git和Github简单教程
  5. Nginx作为代理服务之正反代理
  6. Python之if-else语句
  7. layui弹出层基础参数
  8. 用Buildout来构建Python项目
  9. Nginx负载均衡和HTTPS配置及集群搭建
  10. gluOrtho2D与glViewport