Word Cut

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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 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 exactlyksplit 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

Input
ab
ab
2
Output
1
Input
ababab
ababab
1
Output
2
Input
ab
ba
2
Output
0

Hint

The sought way in the first sample is:

ab → a|b → ba → b|a → ab

In the second sample the two sought ways are:

  • ababab → abab|ab → ababab
  • ababab → ab|abab → ababab

【题意】:

划分一个串:w = xy into u = yx(整体顺序改变,内容不变)

问有多少种方式,使得正好经过K次划分,从原串变成目的串。

【解题思路】:

比较典型的计数DP。

观察到划分只是改变相对顺序,如果把串看成首尾相接的环,则划分只是改变起始点。

转移过程:(经过i次划分,有多少种方式到这个串)

起始:原串方式数=1;其他串方式数=0

第一次:原串=0(因为不能变成自己);其他=1(从起始的原串而来)

第二次:原串=n-1(所有其它串都可以变回来);其他=n-2(除去本身的其他+原串)

……

DP状态应该是划分的次数k和起始点i

但是用滚动数组可以消去划分次数这个维度;所以实现起来就是两个数(分别代表原串和其他)在不停迭代。

最后比较哪些起点对应目标串,做一个累加即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define LL long long
#define maxn 10010
#define inf 0x3f3f3f3f
#define mod 1000000007
#define IN freopen("in.txt","r",stdin);
using namespace std; int main(int argc, char const *argv[])
{
//IN; string a,b;
cin >> a >> b;
int s = a.size();
int n;
cin >> n; LL ans1 = ;
LL ans2 = ;
LL tmp = ;
for(int i=; i<=n; i++){
tmp = ans1;
ans1 = (ans2 * (s-))%mod;
ans2 = (tmp + (ans2*(s-))%mod)%mod;
} LL ans = ;
for(int i=; i<s; i++){
int j;
for(j=i; j<i+s; j++){
if(b[j-i] != a[j%s]) break;
}
if(j == i+s){
if(!i) ans = (ans+ans1)%mod;
else ans = (ans+ans2)%mod;
}
} printf("%I64d\n", ans); return ;
}

CodeForces 176B Word Cut (计数DP)的更多相关文章

  1. CodeForces 176B - Word Cut 计数DP

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

  2. CodeForces 176B Word Cut dp

    Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...

  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 176B 经典DP

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

  8. HDU5800 To My Girlfriend 背包计数dp

    分析:首先定义状态dp[i][j][s1][s2]代表前i个物品中,选若干个物品,总价值为j 其中s1个物品时必选,s2物品必不选的方案数 那么转移的时候可以考虑,第i个物品是可选可可不选的 dp[i ...

  9. [DP之计数DP]

    其实说实在 我在写这篇博客的时候 才刚刚草了一道这样类型的题 之前几乎没有接触过 接触过也是平时比赛的 没有系统的做过 可以说0基础 我所理解的计数dp就是想办法去达到它要的目的 而且一定要非常劲非常 ...

随机推荐

  1. Android真机调试的时候logcat中无法输出调试信息的解决办法

    真机调试不输出日志到logcat的原因是手机厂商默认关闭了调试打印的功能,通过以下方法开启此方法. 下面以华为P6手机为例进行操作: 1.在拨号界面输入:*#*#2846579#*#* 进入测试菜单界 ...

  2. VirtualBox虚拟机剪贴板共享

    默认VirtualBox的虚拟机和主机的剪贴板和拖拽不会共享的,因此需要我们进行处理. 一般情况下,我们选中虚拟机,然后设置剪贴板和拖拽为双向就ok了,但是我这里并没有好,设置如下图: 当我设置上面的 ...

  3. Codeforces Round #237 (Div. 2) B. Marathon(卡long long)

    题目:http://codeforces.com/contest/404/problem/B #include <iostream> #include <cstring> #i ...

  4. js自动判断浏览器类型跳转到手机版

    //电脑版头部写法:<script language="javascript"> function is_mobile() { var regex_match = /( ...

  5. Android基础_3 Activity相对布局

    相对布局要比前面讲的线性布局和表格布局要灵活一些,所以平常用得也是比较多的.相对布局控件的位置是与其周围控件的位置相关的,从名字可以看出来,这些位置都是相对的,确定出了其中一个控件的位置就可以确定另一 ...

  6. 实现推送功能APP端需要完成的工作

    推送功能简介 实现推送的流程如下: 从APP注册推送功能,到APNS服务器发送推送消息给设备,有五个步骤. 一旦推送注册完成,应用自身的服务器以provider的身份提供推送. APP端实现 在代码方 ...

  7. 【转】个人常用iOS第三方库以及XCode插件介绍 -- 不错

    原文网址:http://adad184.com/2015/07/08/my-favorite-libraries-and-plugins/ 第三方库是现在的程序员离不开的东西 不光是APP开发 基本上 ...

  8. 获取某月第一天,最后一天的sql server脚本 【转】http://blog.csdn.net/chaoowang/article/details/9167969

    这是计算一个月第一天的SQL 脚本:    SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一天 SELECT DATEADD(mm, DA ...

  9. jquery生成二维码

    下载Demo js下载: jquery-1.8.3.js .qrcode.js.jquery.qrcode.js <!DOCTYPE html> <html> <head ...

  10. android adt与android sdk有什么关系,他们在开发中各起到什么作用

    ADT(Android Development Tools):目前Android开发所用的开发工具是Eclipse,在Eclipse编译IDE环境中,安装ADT,为Android开发提供开发工具的升级 ...