CodeForces 176B Word Cut dp
Word Cut
题目连接:
http://codeforces.com/problemset/problem/176/C
Description
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 word w = 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 ksplit operations 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.
Input
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.
Output
Print a single number — the answer to the problem. As this number can be rather large, print it modulo 1000000007(109 + 7).
Sample Input
ab
ab
2
Sample Output
1
Hint
题意
给你一个字符串a,然后给你一个字符串b
你可以选择a串的某个位置砍掉,然后再把那个串从后面接到前面
让你砍k次,问你有多少种砍法,从a串砍成b串
题解:
dp
dp[i][0]表示砍了i次的变成b串的方案数
dp[i][1]表示砍了i次的变成非b串的方案数
然后转移就好了,预处理一下有多少个开头是b串的
然后转转就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
long long dp[300000][2];
string a,b;
int k;
int main()
{
cin>>a>>b;
scanf("%d",&k);
if(a==b)dp[0][0]=1;
else dp[0][1]=1;
int x = 0;
for(int i=0;i<a.size();i++)
{
int flag = 1;
for(int j=0;j<a.size();j++)
{
if(a[(i+j)%a.size()]!=b[j])
{
flag = 0;
break;
}
}
if(flag)x++;
}
for(int i=0;i<k;i++)
{
dp[i+1][0]=(x*dp[i][1]+(x-1)*dp[i][0])%mod;
dp[i+1][1]=((a.size()-x)*dp[i][0]+(a.size()-x-1)*dp[i][1])%mod;
}
printf("%d\n",dp[k][0]);
}
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
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]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 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- Codeforces 189A:Cut Ribbon(完全背包,DP)
time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : ...
- Codeforces 176B 经典DP
非常好的一个题目,CF上的DP都比较经典 题意就是 给定一个串A,B,正好执行K次操作,每次操作可以把 A串从中间切开,并调换两部分的位置,问最后得到B串共有多少种不同的切法(只要中间有一次不同,即视 ...
随机推荐
- Eclipse插件安装的三种方法
转自:http://www.blogjava.net/tangzurui/archive/2008/06/30/211669.html 整理了一下格式. (前两种安装方式以多国语言包的安装为例) 1 ...
- hdu 2602 Bone Collector(01背包)
题意:给出包裹的大小v,然后给出n块骨头的价值value和体积volume,求出一路下来包裹可以携带骨头最大价值 思路:01背包 1.二维数组(不常用 #include<iostream> ...
- XML序列化成对象
这个是和ALM上传测试结果结合使用的//把xml序列化成对象以及把对象序列化成xml using System; using System.Data; using System.Configurati ...
- 部署测试环境(ubuntu+mysql+tomcat)
背景:入职新公司,广州这边没有测试开发环境,需要自己搭建一个:要求ubuntu+mysql+tomcat有具体版本要求: 2015/4/13 下载Ubuntu12.04 http://mirror ...
- javascript的变态位运算
javascript的变态位运算 var a = "10" | 0; alert(a); alert (typeof a);结果为10,number. 这就是说这条语句可以将字符串 ...
- 对人脑处理视觉的描述(摘《学习OpenCV(中文版)》)
人脑将视觉信号划分入很多个通道,将各种不同的信息输入你的大脑.你的大脑有一个关注系统,会根据任务识别出图像的重要部分,并做重点分析,而其他部分则分析得较少 .在人类视觉流中存在大量的反馈,但是目前我们 ...
- Tomcat启动报错:Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"]
在用MyEclipse做开发,启动Tomcat的时候,控制台老是报错Failed to initialize end point associated with ProtocolHandler [&q ...
- InfiniBand
Mellanox InfiniBand卡线缆性能延迟性测试程序源码,C源码实现操作mysql库,实现简单的增删改查,代码当前用的是增插入20000条数据 具体见源码 #include <mysq ...
- 【转】jsp页面中jstl标签详解
原文地址: JSLT标签库,是日常开发经常使用的,也是众多标签中性能最好的.把常用的内容,放在这里备份一份,随用随查.尽量做到不用查,就可以随手就可以写出来.这算是Java程序员的基本功吧,一定要扎实 ...
- HDU 1079 Calendar Game(简单博弈)
Calendar Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...