[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& ...
随机推荐
- [BZOJ 4419][Shoi2013]发微博
4419: [Shoi2013]发微博 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 665 Solved: 364[Submit][Status] ...
- Idea简单SpringMVC框架配置
前边已经介绍过了Struts在Idea上的配置,相对于Struts来说,我觉得SpringMVC有更多的优势,首先Struts是需要对action进行配置,页面发送不同的请求,就需要配置不同的acti ...
- C语言博客作业--一二维数组
一.PTA实验作业 题目1(7-6) (1).本题PTA提交列表 (2)设计思路 //天数n:数组下标i:小时数h,分钟数m:对应书号的标签数组flag[1001] //总阅读时间sum初始化为0,借 ...
- Flask 学习 八 用户角色
角色在数据库中表示 app/models.py class Role(db.Model): __tablename__='roles' id = db.Column(db.Integer,primar ...
- [Android FrameWork 6.0源码学习] View的重绘过程之WindowManager的addView方法
博客首页:http://www.cnblogs.com/kezhuang/p/关于Activity的contentView的构建过程,我在我的博客中已经分析过了,不了解的可以去看一下<[Andr ...
- C# reportview 按时间改变行颜色
//) AND ((Day(Now()) - Day() AND (Day(Now()) - Day()),) AND (Day(Now()) - Day()) OR (Month(Now()) - ...
- mongo数据库的常见操作
连接mongodb数据库的命令查看对应数据库mongo.exeuse shujukuming;db.opportunity.findOne({"id":5}); db.opport ...
- java中final 关键字的作用
final 关键字的作用 java中的final关键字可以用来声明成员变量.本地变量.类.方法,并且经常和static一起使用声明常量. final关键字的含义: final在Java中是一个保留的关 ...
- WPF treeview扩展
记录一下工作中遇到的问题,以便以后忘记了可以来看. 在工作中遇到一个问题,就是要实现类型如下的界面,没有使用Telerik和Dev库.本来最开始是想使用Datagrid,但不知道怎么实现treevie ...
- WPF 自定义RadioButton样式
一.RadioButton基本样式 RadioButton基本样式包含两种状态,这里也是使用两张图片来代替两种状态,当然你也可以通过IconFont或Path来替换这两种状态. 效果如下: 样式代码如 ...