CodeForces 176B - Word Cut 计数DP
Let's consider one interesting word game. In this game you should transform one word into another through special operations.
Let's say we have word w, let's split this word into two non-empty parts x and y so, that w = xy. A split operation is transforming wordw = xy into word u = yx. For example, a split operation can transform word "wordcut" into word "cutword".
You are given two words start and end. Count in how many ways we can transform word start into word end, if we apply exactly k splitoperations consecutively to word start.
Two ways are considered different if the sequences of applied operations differ. Two operation sequences are different if exists such number i (1 ≤ i ≤ k), that in the i-th operation of the first sequence the word splits into parts x and y, in the i-th operation of the second sequence the word splits into parts a and b, and additionally x ≠ a holds.
The first line contains a non-empty word start, the second line contains a non-empty word end. The words consist of lowercase Latin letters. The number of letters in word start equals the number of letters in word end and is at least 2 and doesn't exceed 1000 letters.
The third line contains integer k (0 ≤ k ≤ 105) — the required number of operations.
Print a single number — the answer to the problem. As this number can be rather large, print it modulo 1000000007 (109 + 7).
ab
ab
2
1
ababab
ababab
1
2
ab
ba
2
0
The sought way in the first sample is:
ab → a|b → ba → b|a → ab
题意:
给定一个串A,B,正好执行K次操作,每次操作可以把 A串从中间切开,并调换两部分的位置,问最后得到B串共有多少种不同的切法(只要中间有一次不同,即视为不同)
题解:
设定DP[i][0] i次操作后 是B串 的个数
DP[i][1] i次操作后非B串的个数
我们先找出任意由A循环(当成一个环)一个串至多能 转化成B串的数量x
那么 从dp[i-1][0] 上一次操作的B串数量 可以再次转化成B串的数量就是 (x-1) * dp[i-1][0];那么从上一次操作由非B串转化成B串的数量是 (已经求好) x*(dp[i-1][1])
那么类似计数法 求dp[i][1] 转换到K就好了
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<vector>
using namespace std ;
typedef long long ll; const int N = + ;
const int mod = 1e9 + ;
ll dp[N][]; // [i][0]经过i次转换 是B串的个数 // [i][1]经过i次转换 是原串的个数
int main() {
char a[N],b[N];
int k;
scanf("%s%s%d",a,b,&k);
int len = strlen(a);
ll x = ,sum;
for(int i = ; i < len; i++) {
sum = ;int cnt= ;
for(int j = i ;j < len; j++) {
if(a[j] != b[cnt++]) sum++;
}
for(int j = ;j < i; j++) if(a[j] != b[cnt++]) sum++;
if(!sum) x++;
}
if(strcmp(a,b) == ) dp[][] = ;
else dp[][] = ;
for(int i=;i<k;i++)
{
dp[i+][]=(x*dp[i][]+(x-)*dp[i][])%mod;
dp[i+][]=((len-x)*dp[i][]+(len-x-)*dp[i][])%mod;
printf("%I64d %I64d\n",dp[i][],dp[i][]);
}
printf("%I64d\n",dp[k][]);
return ;
}
CodeForces 176B - Word Cut 计数DP的更多相关文章
- 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 ...
- 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]Word Cut
Description 题库链接 给你两个字符串 \(S\) 和 \(T\) ,准许你 \(k\) 次操作,每次将字符串左右分成两个非空的部分,再交换位置,问你有多少种不同的操作方法将 \(S\) 串 ...
- Codeforces 176B【计数DP】
题意: 给你两个串s1,s2和一个K, 有一种操作是在一个串切开然后交换位置, 问s1有多少种方法经过K次这样的操作变成s2: 思路: (从来没接触过计数DP...还是太菜...参考了[大牛blog] ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- Codeforces 176B 经典DP
非常好的一个题目,CF上的DP都比较经典 题意就是 给定一个串A,B,正好执行K次操作,每次操作可以把 A串从中间切开,并调换两部分的位置,问最后得到B串共有多少种不同的切法(只要中间有一次不同,即视 ...
- HDU5800 To My Girlfriend 背包计数dp
分析:首先定义状态dp[i][j][s1][s2]代表前i个物品中,选若干个物品,总价值为j 其中s1个物品时必选,s2物品必不选的方案数 那么转移的时候可以考虑,第i个物品是可选可可不选的 dp[i ...
- [DP之计数DP]
其实说实在 我在写这篇博客的时候 才刚刚草了一道这样类型的题 之前几乎没有接触过 接触过也是平时比赛的 没有系统的做过 可以说0基础 我所理解的计数dp就是想办法去达到它要的目的 而且一定要非常劲非常 ...
随机推荐
- hdu2688 Rotate(树状数组)
题目链接:pid=2688">点击打开链接 题意描写叙述:对一个长度为2<=n<=3000000的数组,求数组中有序对(i<j而且F[i]<F[j])的数量?其 ...
- Cocos2d-x 3.0 红孩儿私家必修 - 第一章 初识Cocos2d-x 3.0project
第一章 初识Cocos2d-x 3.0project Cocos2d-x 3.0出来了,听说与之前版本号相比修改较大 做为一个游戏开发人员.我们应该欢迎Cocos2d-x持续的更新和强大,Coc ...
- unknown argument: '-websockets'
找到building setting找到other link flgs里把里面'-websockets删掉
- UIimageView和UIimage的小区别
UIimageView 用来显示一张图片或者显示一组动画图片 UIimage 不是一个控件,只是一个普通的类,用来生成一张图片,只单纯的生成一张图片,图片只会被加载到内存,如果想要让用户 ...
- avformat_find_stream_info函数卡住问题
问题:初始化RTSP流时,在android设备上卡住在avformat_find_stream_info函数,然后程序崩溃. 但其他URL没问题,且同样在代码在iOS上没问题,由于jni调试,也没看到 ...
- (转)Django学习之 第二章:Django快速上手
安装Python 安装Django非常容易.因为Django可以运行在任何可以运行Python的环境中,所以可以以多种方式进行配置. 在本章我们将尝试覆盖几种常见的Django安装场景. Djang ...
- 使用jquery获取ul中当前正在点击的li的索引
<ul class="list"> <li>哈哈</li> <li>呵呵</li> <li>嘻嘻</l ...
- 微信小程序和App的UI设计有什么异同吗?
大家总是把小程序和App放在一起比,因此我也花时间看了一下小程序的开发指南,尤其是UI部分的设计和原则,今天就拿它和苹果的HIG(Human Interface Guidelines)做个比较,其实两 ...
- pixhawk入门知识
Pixhawk是一种先进的自动驾驶仪,由PX4开放硬件项目设计和3D机器人制造.它具有来自ST公司先进的处理器和传感器技术,以及NuttX实时操作系统,能够实现惊人的性能,灵活性和可靠性控制任何自主飞 ...
- C++小程序(1)——文件整理工具
网上下载的漫画是jpg或png之类的图片文件,用系统自带的图片管理器看不方便,想要能把图片想网页一样浏览的功能,找了很多图片管理器也没有带这个功能,于是就自己编写了一个小程序实现. 思想就是在图片目录 ...