codeforces613E
Puzzle Lover
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
code
edoc code
4
aaa
aaa aa
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的更多相关文章
- [Codeforces613E]Puzzle Lover
		
Problem 给你2*n的格子,每个格子有一个字母,从任意一点出发,不重复的经过上下左右,生成要求的字符串.问有几种不同的走法. Solution 分三段,左U型.中间.右U型. 分别枚举左边和右边 ...
 
随机推荐
- Jmeter之设置线程组运行次数/时间
			
线程组的设置 线程组运行的次数=线程数*循环次数 Ramp-Up Period:表示启动时间 例如:线程数:10,循环次数:10,Ramp-Up Period:2 表示,这个线程组一共有100个线程( ...
 - 并不对劲的bzoj4231: 回忆树
			
题目大意 \(n\)个点的树,每条边上有一个小写字母. 操作:给定2个点\(u\),\(v\)(\(u\)可能等于\(v\))和一个非空字符串\(s\),问从\(u\)到\(v\)的简单路径上的所有边 ...
 - docker CMD 和 ENTRYPOINT 区别
			
昨天用Dockerfile来启动mongodb的集群,启动参数--replSet死活没执行,最后就决定研究一哈cmd和entrypoint.但是上网看了一些资料个人觉得讲的不好,还是没有说出根本的东西 ...
 - Git 学习笔记之(三)将本地工程导入到GitHub 仓库中
			
一:操作步骤第一步:建立git仓库 cd到你的本地项目根目录下,执行git命令,此命令会在当前目录下创建一个.git文件夹. git init 第二步:将项目的所有文件添加到仓库中 git add . ...
 - 实现Banner广告图片轮换
			
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
 - vue 编辑
			
点击文字修改 <div class="baseInfo"> <p class="title">基本信息</p> <p ...
 - shell脚本视频学习2
			
一.函数 1.函数格式 2.函数传入参数 3.手动输入函数中的参数 4.函数返回值 成功返回0,失败返回1 5.输入一个目录,判断目录是否存在,如果不存在则给出提示,如果存在则提示输入要创建的文件名, ...
 - 通过Callable接口实现多线程
			
一.通过Callable接口实现多线程 c.实现Callable重写call方法 实现Callable和实现Runnable类似,但是功能更强大,具体表现在 a.可以在任务结束后提供一个返回值,Run ...
 - 【漏洞分析】Discuz! X系列全版本后台SQL注入漏洞
			
0x01漏洞描述 Discuz!X全版本存在SQL注入漏洞.漏洞产生的原因是source\admincp\admincp_setting.php在处理$settingnew['uc']['appid' ...
 - vue中优化CheckBox初始状态被选中问题
			
<template> <div class="hello"> <h2>我是主界面</h2> <!-- <h2>我是 ...
 
			
		