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. Springboot导出Excel并下载

    引入相关依赖 <!--数据导出excel--> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> & ...

  2. axios配置

    import axios, { isCancel } from 'axios' import { md5 } from 'vux' import util from '@/libs/util' imp ...

  3. dev GridView 的组计和分组计

    /// <summary> /// //添加组计 /// </summary> private void SetGroupSummary(GridView gv, string ...

  4. 你不知道的css各类布局(二)之流体布局、液体布局、栅格布局

    流体布局 什么是流 在谈论流体布局之前我们需要知道一件事情就是何为“流”,所谓“流”就是“文档流”,是css中的一种基本定位和布局 概念 流体布局(Liquid/Fluid Layout)指的是利用元 ...

  5. TCP/IP协议栈各个层次及分别的功能

    网络接口层:这是协议栈的最低层,对应OSI的物理层和数据链路层,主要完成数据帧的实际发送和接收.网络层:处理分组在网络中的活动,例如路由选择和转发等,这一层主要包括IP协议.ARP.ICMP协议等.传 ...

  6. BootStrape基础使用

    官网:www.bootcss.com 一. 全局css样式 栅格系统 栅格系统用于通过一系列的行(row)与列(column)的组合来创建页面布局 <!DOCTYPE html> < ...

  7. Linux下MySql基本操作命令

    (1).切换至MySql目录下[假设MySql安装路径为:/home/mysql/bin] cd /home/mysql/bin (2).连接MySql mysql -u用户名 -p,回车后输入密码 ...

  8. 第八章· Redis API 开发

    Redis 开发 1.源码安装Python环境 Python官网:https://www.python.org/ #下载Python3.6.4安装包 [root@db03 ~]# wget https ...

  9. 2.Nginx基本配置

    1. Nginx相关概念 代理服务器一般分为正向代理(通常直接称为代理服务器)和反向代理. 通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发 ...

  10. java-web服务器-tomcat

    HTTP服务器: 一.定义: 1)是一个安装在服务端的[资源文件调度软件] 二.作用: 1)自动解析接收的[请求协议包]内容 2)自动在服务端计算机上定位[被访问的文件] 3)自动创建[Http响应协 ...