[Codeforces 176B]Word Cut
Description
给你两个字符串 \(S\) 和 \(T\) ,准许你 \(k\) 次操作,每次将字符串左右分成两个非空的部分,再交换位置,问你有多少种不同的操作方法将 \(S\) 串变为 \(T\) 串。
\(1\leq k\leq 100000, 1\leq |S|=|T|\leq 1000\)
Solution
容易发现不论经过多少次操作,其操作后的字符串一定是在原字符串上截开两段再拼接而成。
所以不妨记 \(f_{i,j}\) 为操作 \(i\) 次后在 \(j\) 处截开的方案数为 \(f_{i,j}\) 。
转移就是由 \(i-1\) 中所有 \(\neq j\) 的位置转移过来的。
但复杂度是 \(O(|S|k)\) 的,不太优雅。但其实考虑到所有字符串本质只有与 \(T\) 串相不相同的两种情况,我们不妨记 \(f_{i,0/1}\) 表示操作 \(i\) 次后与 \(T\) 串是否相同的方案数为 \(f_{i,j}\) 。
这样就可以线性转移了。但是要先 \(O(|S|^2)\) 预处理出 \(same\) ,表示多少个位置断开与 \(T\) 串本质相同。
Code
#include <bits/stdc++.h>
using namespace std;
const int N = 1000, yzh = 1e9+7;
char s[N+5], t[N+5];
int same, len, f[N*100+5][2], k;
bool check(int x) {
for (int i = 1; i <= len; i++) {
if (s[x] != t[i]) return false;
++x; if (x > len) x = 1;
}
return true;
}
void work() {
scanf("%s%s", s+1, t+1); len = strlen(s+1); scanf("%d", &k);
for (int i = 1; i <= len; i++)
if (check(i)) ++same;
if (check(1)) f[0][0] = 1; else f[0][1] = 1;
for (int i = 1; i <= k; i++) {
f[i][0] = (1ll*f[i-1][0]*(same-1)%yzh+1ll*f[i-1][1]*same%yzh)%yzh;
f[i][1] = (1ll*f[i-1][0]*(len-same)%yzh+1ll*f[i-1][1]*(len-same-1)%yzh)%yzh;
}
printf("%d\n", f[k][0]);
}
int main() {
work(); return 0;
}
[Codeforces 176B]Word Cut的更多相关文章
- CodeForces 176B Word Cut (计数DP)
Word Cut Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit St ...
- CodeForces 176B Word Cut dp
Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...
- CodeForces 176B - Word Cut 计数DP
B. Word Cut Let's consider one interesting word game. In this game you should transform one word i ...
- ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)
Description Let's consider one interesting word game. In this game you should transform one word int ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- codeforces 883H - Palindromic Cut - [字符串处理]
题目链接:http://codeforces.com/problemset/problem/883/H Time limit: 3000 ms Memory limit: 262144 kB Koly ...
- CodeForces 982 C Cut 'em all!
Cut 'em all! 题意:求删除了边之后,剩下的每一块联通块他的点数都为偶数,求删除的边最多能是多少. 题解:如果n为奇数,直接返回-1,因为不可能成立.如果n为偶数,随意找一个点DFS建树记录 ...
- Codeforces 189A:Cut Ribbon(完全背包,DP)
time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : ...
- CodeForces 614A Link/Cut Tree
#include<cstdio> #include<cstring> #include<cmath> #include<stack> #include& ...
随机推荐
- c语言程序设计第3周编程作业(数字特征)
题目内容: 对数字求特征值是常用的编码算法,奇偶特征是一种简单的特征值.对于一个整数,从个位开始对每一位数字编号,个位是1号,十位是2号,以此类推.这个整数在第n位上的数字记作x,如果x和n的奇偶性相 ...
- 『开源重编译』System.Data.SQLite.dll 自适应 x86 x64 AnyCPU 重编译
背景: > System.Data.SQLite.dll 程序集 不能良好的支持 AngCPU 格式 System.Data.SQLite.dll 在 适应 x86 和 x64 有三个方案: & ...
- C语言——第七周作业
题目 题目一:求交错序列前N项和 1.实验代码 #include <stdio.h> int main() { int n , i , b ; , a , c ; scanf(" ...
- oc中protocol、category和继承的区别
OC中protocol.category和继承的区别以前还是有点迷糊,面试的时候说的有点混乱,现在结合一些资料总结一下. 利用继承,多态是一个很好的保持"对扩展开放.对更改封闭"( ...
- 关于collectionView和tableView的两种cell的出列方法的区别
相信好多人一定会对collectionView和tableView的两种cell出列方法有所疑问,下面以UICollection为例子进行举例说明 假设我们已经创建了一个collectionView, ...
- Beta冲刺Day2
项目进展 李明皇 今天解决的进度 优化了信息详情页的布局:日期显示,添加举报按钮等 优化了程序的数据传递逻辑 明天安排 程序运行逻辑的完善 林翔 今天解决的进度 实现微信端消息发布的插入数据库 明天安 ...
- ThreadLocal就是这么简单
前言 今天要研究的是ThreadLocal,这个我在一年前学习JavaWeb基础的时候接触过一次,当时在baidu搜出来的第一篇博文ThreadLocal,在评论下很多开发者认为那博主理解错误,给出了 ...
- GZip 压缩及解压缩
/// <summary> /// GZipHelper /// </summary> public class GZipHelper { /// <summary> ...
- 阿里云API网关(4)快速入门(开放 API)
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- Linux搭建Apache+Tomcat实现负载均衡
一.首先需要安装java,详见http://www.cnblogs.com/fun0623/p/4350004.html 二.编译安装Apache,详见http://www.cnblogs.com/f ...