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就是想办法去达到它要的目的 而且一定要非常劲非常 ...
随机推荐
- hadoop无法启动DataNode问题
因为种种原因,今天又一次安装hadoop集群.清空了/tmp下的文件夹,重新启动集群,hadoop namenode -format 之后 start-all 可是没有发现DataNode的守护 ...
- QT使用tableWidget显示双排列表 而且选中用红框圈出来
如需转载请标明出处:http://blog.csdn.net/itas109 整个project下载地址:http://download.csdn.net/detail/itas109/7607735 ...
- Linux3.5内核以后的路由下一跳缓存
在Linux3.5版本号(包括)之前.存在一个路由cache.这个路由cache的初衷是美好的,可是现实往往是令人遗憾的.下面是陈列得出的两个问题:1.面临针对hash算法的ddos问题(描写叙述该问 ...
- BZOJ 3160: 万径人踪灭 FFT+快速幂+manacher
BZOJ 3160: 万径人踪灭 题目传送门 [题目大意] 给定一个长度为n的01串,求有多少个回文子序列? 回文子序列是指从原串中找出任意个,使得构成一个回文串,并且位置也是沿某一对称轴对称. 假如 ...
- kaggle 中使用ipython
# pandas import pandas as pd from pandas import Series,DataFrame # numpy, matplotlib, seaborn import ...
- Python·Jupyter Notebook各种使用方法记录
标签(空格分隔): Python 一 Jupyter NoteBook的安装 1 新版本Anaconda自带Jupyter 2 老版本Anacodna需自己安装Jupyter 二 更改Jupyter ...
- 18.QT消息链筛选机制以及组合键
mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> 5 #include <Q ...
- BZOJ 1806 DP
思路: f[i][a][b][c][d] 表示在第i天 昨天1矿吃的是a 前天1矿吃的是b 昨天2矿吃的是c 前天2矿吃的是d 的最优解 暴力转移 哦 需要优化一下空间- 变成i%2 就好了 //By ...
- 算法入门经典-第六章 例题6-21 SystemDependencies
题意:软件组件之间会有依赖关系,比如你下一个Codeblocks你也得顺带着把编译器给下上.你的任务是模拟安装和卸载软件组件的过程.有以下五种指令,如果指令为“END”则退出程序:若为以下四种指令,则 ...
- C# 实现透明可移动窗体
1.设置窗体属性 this.BackColor this.TransparencyKey = this.BackColor; 2.窗体加载图片 this.BackgroundImage = globa ...