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. RHEL8运维新利器--Cockpit使用方法

    在web浏览器中查看服务器并使用鼠标执行系统任务,很容易管理存储.配置网络和检查日志等操作. # 安装cockpit yum -y install cockpit # 启用cockpit system ...

  2. Java 反射理解(三)-- Java获取方法信息

    Java 反射理解(三)-- Java获取方法信息 基本的数据类型.void关键字,都存在类类型. 举例如下: public class ClassDemo2 { public static void ...

  3. C# 日期格式化以及日期常用方法

    一.日期格式化 1.ToString() d 月中的某一天.一位数的日期没有前导零. dd 月中的某一天.一位数的日期有一个前导零. ddd 周中某天的缩写名称,在 AbbreviatedDayNam ...

  4. VS2008提示无法打开包括文件:“afxcontrolbars.h”解决办法

    主要造成这个问题的主要原因是,在打了SP1的VS2008下创建的项目放在没有打SP1的VS2008下就会报错   解决办法: 一.给VS2008打上SP1补丁即可      此方法比较彻底,但是安装较 ...

  5. centos 配置mysql主从复制

    mysql+centos7+主从复制   MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公 ...

  6. void in javascript是一个指定要计算表达式但不返回值的运算符

    void in javascript是一个指定要计算表达式但不返回值的运算符.void运算符使用格式如下:1.javascript:void(表达式)2.javascript:void表达式表达式是要 ...

  7. GDI+ 绘图教程 验证码

    使用的 C# winform using System; using System.Collections.Generic; using System.ComponentModel; using Sy ...

  8. php--常见算法3

    <?php function leijia($number){ $arr=[]; for($i=1;$i<=$number;$i++) { for($j=1;$j<=$number; ...

  9. Delphi MSCom安装

  10. 【异常】Caused by: java.lang.IllegalStateException: Zip64 archives are not supported

    1 自己打包Spring boot项目依赖了第三方的Phoenix jar包过大,导致启动后报错 参考了这篇博客:https://cloud.tencent.com/developer/ask/135 ...