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
*/

 

codeforces613E的更多相关文章

  1. [Codeforces613E]Puzzle Lover

    Problem 给你2*n的格子,每个格子有一个字母,从任意一点出发,不重复的经过上下左右,生成要求的字符串.问有几种不同的走法. Solution 分三段,左U型.中间.右U型. 分别枚举左边和右边 ...

随机推荐

  1. LeetCode面试常见100题( TOP 100 Liked Questions)

    LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...

  2. css;js学习(一)

    推荐基础前端学习地址https://ke.qq.com/course/315961蝉壳学院 清除浮动 .clearfix:before,.clearfix:after{ content: " ...

  3. Extjs中,Vo对象中的属性无法在data中获取的解决方法

    store.getById(data.data.id).raw.redpackid

  4. promise使用的正确方式

    一开始恨不能理解下面的代码,为什么可以一直then下去,什么时候要直接return  xxx,什么时候return 一个promise,什么时候用Promise.resolve() function ...

  5. Redis之淘汰策略

    Redis 内存数据集大小上升到一定大小的时候,就会进行数据淘汰策略. Redis 提供了 6 种数据淘汰策略: 1. volatile-lru:从已设置过期时间的数据集中挑选最近最少使用的数据淘汰. ...

  6. Android-分享多图到微信好友

    /** * 微信分享(多图片) */ private static void shareWeChatByImgList(String kDescription, List<File> im ...

  7. MySQL表内更新时,自动记录时间

    1.创建表: create table test_time(id int primary key not null,status  varchar(24),create_time datetime d ...

  8. 链接进入react二级路由,引发的子组件二次挂载

    这个问题很怪,我两个二级路由从链接进入的时候,会挂载两次子组件. 从链接进入,是因为新页面在新标签页打开的. 有子组件是因为公共组件提取 同样的操作,有一些简单的二级路由页面,就不会挂载两次. 讲道理 ...

  9. php生成器yield

    上次说了php的生成器Iterator,这次说一下yield 迭代生成器 (迭代)生成器也是一个函数,不同的是这个函数的返回值是依次返回, 而不是只返回一个单独的值.或者,换句话说,生成器使你能更方便 ...

  10. Java数据结构浅析

    程序 = 数据结构 + 算法 本文概述Java中常用的数据结构,并简述其使用场景 1. 数据结构的定义 数据结构是一种逻辑意义,指的是逻辑上的数据组织方式及相应的处理,与数据在磁盘的具体存储方式不完全 ...