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的更多相关文章

  1. CodeForces 176B Word Cut (计数DP)

    Word Cut Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  2. CodeForces 176B - Word Cut 计数DP

    B. Word Cut   Let's consider one interesting word game. In this game you should transform one word i ...

  3. ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)

    Description Let's consider one interesting word game. In this game you should transform one word int ...

  4. [Codeforces 176B]Word Cut

    Description 题库链接 给你两个字符串 \(S\) 和 \(T\) ,准许你 \(k\) 次操作,每次将字符串左右分成两个非空的部分,再交换位置,问你有多少种不同的操作方法将 \(S\) 串 ...

  5. Codeforces 176B【计数DP】

    题意: 给你两个串s1,s2和一个K, 有一种操作是在一个串切开然后交换位置, 问s1有多少种方法经过K次这样的操作变成s2: 思路: (从来没接触过计数DP...还是太菜...参考了[大牛blog] ...

  6. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  7. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  8. Codeforces 189A:Cut Ribbon(完全背包,DP)

    time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : ...

  9. Codeforces 176B 经典DP

    非常好的一个题目,CF上的DP都比较经典 题意就是 给定一个串A,B,正好执行K次操作,每次操作可以把 A串从中间切开,并调换两部分的位置,问最后得到B串共有多少种不同的切法(只要中间有一次不同,即视 ...

随机推荐

  1. zend+xdebug单步调试

    也允许使用第三方调试工具,今天以PHP教程形式分享如何使用zend studio配置Xdebug来调试PHP程序. 使用Xdebug在zend studio中调试PHP源码之前,请务必安装配置Xdeb ...

  2. Redis pipeline and list

    Redis Redis 是一个开源的基于内存的数据结构存储器.通常可作为数据库,缓存和消息中介.它支持的数据结构有:字符串.哈希表.列表.集合.支持范围查询的有序集合.位图.hyperloglogs和 ...

  3. Ruby相关图书推荐

    Ruby基础教程第4版 作      者 [日] 高桥征义,[日] 后藤裕藏 著:何文斯 译:[日] 松本行弘 校 出 版 社 人民邮电出版社 出版时间 2014-09-01 版      次 4 页 ...

  4. 渗透测试实例Windows XP SP2

    一.msf> use exploit/windows/dcerpc/ms03_026_dcom.看到命令提示符的改变表明该命令已经运行成功. 二.为漏洞利用代码设置必要的参数,show opti ...

  5. 判断是否为BST

    递归的方法,用返回false的方法.中序遍历的想法很好,空间浪费.遍历的过程记录上一次的值进行比较. //题目描述 // //请实现一个函数,检查一棵二叉树是否为二叉查找树. //给定树的根结点指针T ...

  6. stardict

    1.下载词典文件: 2.把下载到的文件移动到/tmp目录下 # mv stardict-*.bz2 /tmp 3.解压缩 # tar jxf stardict-oxford-gb-2.4.2.tar. ...

  7. Hadoop学习记录(7)|Eclipse远程调试Hadoop

    1.创建Hadoop项目 2.创建包.类 这里使用hdfs.WordCount为例 3.编写自定Mapper和Reducer程序 MyMapper类 static class MyMapper ext ...

  8. Node-APN 开源推送服务

    Node-APN是一个开放的结合了苹果推送通知的Node.js模块,该源码模块使用简单,反馈服务支持.错误处理,在发送出错时自动重发.遵从苹果的最佳实践. Node-APN(github)

  9. Ubuntu14.04上安装pip的方法

    在Ubuntu14.04上,建议通过下面的方法安装,这是一种通用的方法,也适用于Windows,当然在Windows下 手动下载下来就行了 wget https://bootstrap.pypa.io ...

  10. <转载>linux下内存泄露查找、BUG调试

    先收藏着,抽空好好看看:http://www.ibm.com/developerworks/cn/linux/l-pow-debug/ 简介 调试程序有很多方法,例如向屏幕上打印消息,使用调试器,或者 ...