题意

英文版题面

<style>
.input-output-copier {
font-size: 1.2rem;
float: right;
color: #888 !important;
cursor: pointer;
border: 1px solid rgb(185, 185, 185);
padding: 3px;
margin: 1px;
line-height: 1.1rem;
text-transform: none;
} .input-output-copier:hover {
background-color: #def;
} .test-explanation textarea {
width: 100%;
height: 1.5em;
}
</style>
E. Puzzle Lover
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 k small 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
Copy
code
edoc

code
Output
Copy
4
Input
Copy
aaa
aaa

aa
Output
Copy
14
            </div>

阿狸的矩阵字符串匹配

Background

阿狸利用糖果稠密度分析仪得到了许多糖果,也终于成功地离开了基环内向树森林。刚走出森林的他却又落入了魔法陷阱之中。陷阱中有一个写满字母的矩阵,似乎只有找到合适的不重复路径,并在上面走一遍,才能解除陷阱。

可是合适的路径到底是什么呢?阿狸摇晃着自己的小脑袋,只感觉有水在流动,思考似乎成了奢侈的事,魔法陷阱中的 debuff 太强了。

Description

阿狸所看到的是一个 2×N 的矩阵 A,矩阵中每个格子都是一个小写字母。同时,你得到了长度为 M 一个字符串 S,你需要在矩阵中找到一条不重复路径(起点和终点任意),使得依次经过的字母连起来恰好是 S,求这样的路径有多少种。

你只能向上、向下、向左或向右走,不能斜着走或跳着走,也不能走出矩阵外或重复经过同一个点。两种路径不同,当且仅当至少有一个时刻所在的位置不同。

由于答案可能很大,你只要输出答案对 \(10^9+7\) 取模的值即可。

Input

第一行和第二行两行长度相同的字符串描述 2×N 的矩阵 A。

第三行一个空行。

第四行一个字符串,表示 S。

Output

一个正整数表示答案对 \(10^9+7\) 取模后的值。

Sample Input1

code

edoc

code

Sample Output1

4

Sample Input2

aaa

aaa

aa

Sample Output2

14

Sample Explanation



Data Limitation

对于测试点 1,保证 N≤5。

对于测试点 2~3,保证 N≤10。

对于测试点 4,保证 N≤20。

对于测试点 5~6,保证 N≤50。

对于测试点 7~8,保证 N≤300。

对于测试点 9~10,保证 N≤1,000。

对于测试点 11~12,保证 S 是一个全 a 字符串,且 A 也是全 a 的矩阵。

对于测试点 13~14,保证 S 是一个全 a 字符串。

对于测试点 15~16,保证 S 是一个形如 abbbb…的仅由一个字符 a 和若干字符 b 组成

的字符串。

对于 100%的数据,保证 \(1≤N,M≤2×10^3\)。

分析

\(2\times N\)的矩阵,总的折返次数不会超过2,分成左中右三部分分别处理。

可以发现不重复经过同一个格子的路径一定是形如这样的:



这个路径可以分成三段:

➢ 从 S 出发向左走一段再回来。

➢ 上下上下地往右走。

➢ 往右走一段再回到 T。

当然,S 和 T 的位置可以调换。

发现这个性质之后就可以直接 DP 了,左右两段可以用字符串 Hash 做,Left[i][j][k]表示匹配到第 i 行第 j 列的位置,匹配了 k 个字符的方案,那么 Left[i][j][k]的转移就是Left[i][j][k]=Left[i][j-1][k]+1;Right[i][j][k]表示匹配到第 i 行第 j 列的位置,匹配了 k 个字符的方案,那么 Right[i][j][k]的转移就是 Right[i][j][k]= Right[i][j+1][k]+1。接着中间的一段用简单的 DP 实现,设 F[i][j][k]表示在第 i 行第 j 列的位置,匹配到第 k 个字符的方案,把三段拼起来就好了。这题就这么简单,主要是细节处理上比较麻烦。

总复杂度是 \(O(N^2)\)。

代码

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll; co int N=2020,P1=1e9+7,P2=1e9+9;
int n,m,ans;
int tm1[N],tm2[N],tm1_[N],tm2_[N];
int f[2][N][N];
struct dd{ // double hash
int x,y;
bool operator==(co dd&n)co {return x==n.x&&y==n.y;}
}t1,t2,t3,t4;
struct cc{
char s[N];
int len,pre1[N],pre2[N],suf1[N],suf2[N];
void read(){
scanf("%s",s+1),len=strlen(s+1);
for(int i=1;i<=len;++i){
pre1[i]=((ll)tm1[i-1]*s[i]+pre1[i-1])%P1;
pre2[i]=((ll)tm2[i-1]*s[i]+pre2[i-1])%P2;
}
for(int i=len;i;--i){
suf1[i]=((ll)tm1[len-i]*s[i]+suf1[i+1])%P1;
suf2[i]=((ll)tm2[len-i]*s[i]+suf2[i+1])%P2;
}
}
dd get_hash(int l,int r){
dd t;
if(l<=r){
t.x=(ll)tm1_[l-1]*(pre1[r]+P1-pre1[l-1])%P1;
t.y=(ll)tm2_[l-1]*(pre2[r]+P2-pre2[l-1])%P2;
}
else{ // upside down
t.x=(ll)tm1_[len-l]*(suf1[r]+P1-suf1[l+1])%P1;
t.y=(ll)tm2_[len-l]*(suf2[r]+P2-suf2[l+1])%P2;
}
return t;
}
}s1,s2,w;
int ksm(int x,int y,int P){
int z=1;
for(;y;y>>=1,x=(ll)x*x%P)
if(y&1) z=(ll)z*x%P;
return z;
}
void init(){
int n=2000;
tm1[0]=tm2[0]=tm1_[0]=tm2_[0]=1;
for(int i=1;i<=n;++i) tm1[i]=31LL*tm1[i-1]%P1,tm2[i]=31LL*tm2[i-1]%P2;
tm1_[n]=ksm(tm1[n],P1-2,P1),tm2_[n]=ksm(tm2[n],P2-2,P2);
for(int i=n-1;i;--i) tm1_[i]=31LL*tm1_[i+1]%P1,tm2_[i]=31LL*tm2_[i+1]%P2;
s1.read(),s2.read(),w.read();
}
int main(){
// freopen("string.in","r",stdin),freopen("string.out","w",stdout);
init();
n=s1.len,m=w.len;
// forward
f[0][n+1][0]=f[1][n+1][0]=1; // right
for(int i=n;i;--i){
f[0][i][0]=f[1][i][0]=1;
for(int k=2;k+k<=m&&i+k-1<=n;++k){ // length starts with 2
t1=s1.get_hash(i,i+k-1),t2=s2.get_hash(i,i+k-1);
t3=w.get_hash(m,m-k+1),t4=w.get_hash(m-k-k+1,m-k);
if(t1==t3&&t2==t4) f[1][i][k+k]=1;
if(t2==t3&&t1==t4) f[0][i][k+k]=1;
}
}
for(int i=n;i;--i) // middle
for(int k=1;k<=m;++k){
if(s1.s[i]==w.s[m-k+1]) (f[0][i][k]+=f[0][i+1][k-1])%=P1;
if(s2.s[i]==w.s[m-k+1]) (f[1][i][k]+=f[1][i+1][k-1])%=P1;
if(k>1&&s1.s[i]==w.s[m-k+1]&&s2.s[i]==w.s[m-k+2]) (f[0][i][k]+=f[1][i+1][k-2])%=P1;
if(k>1&&s2.s[i]==w.s[m-k+1]&&s1.s[i]==w.s[m-k+2]) (f[1][i][k]+=f[0][i+1][k-2])%=P1;
}
for(int i=1;i<=n+1;++i){
(ans+=f[0][i][m])%=P1;
(ans+=f[1][i][m])%=P1;
for(int k=2;k+k<=m&&k<i;++k){ // left
t1=s1.get_hash(i-k,i-1),t2=s2.get_hash(i-k,i-1);
t3=w.get_hash(k,1),t4=w.get_hash(k+1,k+k);
if(t1==t3&&t2==t4) (ans+=f[1][i][m-k-k])%=P1;
if(t2==t3&&t1==t4) (ans+=f[0][i][m-k-k])%=P1;
}
}
if(m==1) return printf("%d\n",ans),0; // no inverse
memset(f,0,sizeof f);
f[0][n+1][0]=f[1][n+1][0]=1;
for(int i=n;i;--i){
f[0][i][0]=f[1][i][0]=1;
for(int k=2;k+k<m&&i+k-1<=n;++k){ // <m to avoid repeating
t1=s1.get_hash(i,i+k-1),t2=s2.get_hash(i,i+k-1);
t3=w.get_hash(1,k),t4=w.get_hash(k+k,k+1);
if(t1==t3&&t2==t4) f[1][i][k+k]=1;
if(t2==t3&&t1==t4) f[0][i][k+k]=1;
}
}
for(int i=n;i;--i)
for(int k=1;k<=m;++k){
if(s1.s[i]==w.s[k]) (f[0][i][k]+=f[0][i+1][k-1])%=P1;
if(s2.s[i]==w.s[k]) (f[1][i][k]+=f[1][i+1][k-1])%=P1;
if(m>2&&k>1&&s1.s[i]==w.s[k]&&s2.s[i]==w.s[k-1]) (f[0][i][k]+=f[1][i+1][k-2])%=P1; // m>2 to AR
if(m>2&&k>1&&s2.s[i]==w.s[k]&&s1.s[i]==w.s[k-1]) (f[1][i][k]+=f[0][i+1][k-2])%=P1;
}
for(int i=1;i<=n+1;++i){
(ans+=f[0][i][m])%=P1;
(ans+=f[1][i][m])%=P1;
for(int k=2;k+k<m&&k<i;++k){ // <m to AR
t1=s1.get_hash(i-k,i-1),t2=s2.get_hash(i-k,i-1);
t3=w.get_hash(m-k+1,m),t4=w.get_hash(m-k,m-k-k+1);
if(t1==t3&&t2==t4) (ans+=f[1][i][m-k-k])%=P1;
if(t2==t3&&t1==t4) (ans+=f[0][i][m-k-k])%=P1;
}
}
return printf("%d\n",ans),0;
}

CF613E Puzzle Lover的更多相关文章

  1. 题解 CF613E Puzzle Lover

    解题思路 其实仔细观察我们可以发现路径一定是一个类似于下图的一个左括号之后中间随便反复曲折,然后右边在来一个右括号. 然后对于两个括号形状的东西其实是可以利用 Hash 来判等特殊处理的. 对于中间的 ...

  2. [Codeforces613E]Puzzle Lover

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

  3. cf 613E - Puzzle Lover

    Description 一个\(2*n\)的方格矩阵,每个格子里有一个字符 给定一个长度为\(m\)的字符串\(s\) 求在方格矩阵中,有多少种走法能走出字符串\(s\) 一种合法的走法定义为:从任意 ...

  4. 多校联训 DP 专题

    [UR #20]跳蚤电话 将加边变为加点,方案数为 \((n-1)!\) 除以一个数,\(dp\) 每种方案要除的数之和即可. 点击查看代码 #include<bits/stdc++.h> ...

  5. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  6. codeforces613E

    Puzzle Lover CodeForces - 613E Oleg Petrov loves crossword puzzles and every Thursday he buys his fa ...

  7. Puzzle 面向服务/切面(AOP/IOC)开发框架 For .Net

    Puzzle 面向服务/切面AOP开发框架 For .Net AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效 ...

  8. HDU5456 Matches Puzzle Game(DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5456 Description As an exciting puzzle game for ...

  9. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

随机推荐

  1. MySQL mysql server与存储引擎

    mysql server系统架构 逻辑模块组成: mysql逻辑模块可以分为两层架构,第一层是sql layer主要包括权限判断.sql解析.执行计划优化.query cache的处理等:第二层是存储 ...

  2. sqlalchem表关联(一对多,一对一,多对多)

    简介: 一:一对多关系 1.表示一对多的关系时,在子表类中通过 foreign key (外键)限制本列的值,然后,在父表类中通过 relationship() 方法来引用子表的类. 2.示例代码: ...

  3. eclipse package视图和navigator视图的区别

    package视图是适合开发的视图,因为开发时我们只关注源文件,并不关注编译后的二进制文,所有在该视图中存放二进制文件的classes文件被隐藏了,而navigator视图,就是项目在工作空间中存放的 ...

  4. Linux搭建Hadoop集群---Jdk配置

    三台虚拟机:master slave1 slave2 192.168.77.99 master 192.168.77.88 slave1 192.168.77.77 slave2   1.修改主机名: ...

  5. Java获取系统时间的四种方法

    1.Date day=new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ...

  6. substr、substring和slice的区别

    substr(start,[length])表示从start位置开始取length个字符串:substring(start,end)表示从start,到end之间的字符串,包括start位置的字符但是 ...

  7. 五. Python基础(5)--语法

    五. Python基础(5)--语法 1 ● break结束的是它所在的循环体, continue是让它所在的循环体继续循环 # 打印: 1 10 2 10 3 10 4 10 5 10 6 10 7 ...

  8. 4.1 C++多态的概念及前提条件

    参考:http://www.weixueyuan.net/view/6370.html 总结: 而多态的功能则是将函数名动态绑定到函数入口地址,这样的动态绑定过程称为运行期绑定. 而在运行期绑定的函数 ...

  9. 进阶ES6 点滴认知

    1.let--不允许重复声明 根据http://es6.ruanyifeng.com/#docs/let 的例子,我竟然 报格式错误 说实话,我也没见过函数这样的写法......然后我就随意加了函数名 ...

  10. connection reset 分析解决(转载)

    文章转自:https://my.oschina.net/xionghui/blog/508758;记录下来以便以后复习查阅; 在使用HttpClient调用后台resetful服务时,“Connect ...