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
  1. ab
    ab
    2
Output
  1. 1
Input
  1. ababab
    ababab
    1
Output
  1. 2
Input
  1. ab
    ba
    2
Output
  1. 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

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

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<vector>
  7. #define LL long long
  8. #define maxn 10010
  9. #define inf 0x3f3f3f3f
  10. #define mod 1000000007
  11. #define IN freopen("in.txt","r",stdin);
  12. using namespace std;
  13.  
  14. int main(int argc, char const *argv[])
  15. {
  16. //IN;
  17.  
  18. string a,b;
  19. cin >> a >> b;
  20. int s = a.size();
  21. int n;
  22. cin >> n;
  23.  
  24. LL ans1 = ;
  25. LL ans2 = ;
  26. LL tmp = ;
  27. for(int i=; i<=n; i++){
  28. tmp = ans1;
  29. ans1 = (ans2 * (s-))%mod;
  30. ans2 = (tmp + (ans2*(s-))%mod)%mod;
  31. }
  32.  
  33. LL ans = ;
  34. for(int i=; i<s; i++){
  35. int j;
  36. for(j=i; j<i+s; j++){
  37. if(b[j-i] != a[j%s]) break;
  38. }
  39. if(j == i+s){
  40. if(!i) ans = (ans+ans1)%mod;
  41. else ans = (ans+ans2)%mod;
  42. }
  43. }
  44.  
  45. printf("%I64d\n", ans);
  46.  
  47. return ;
  48. }

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. OutputStream窥探

    /* * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...

  2. toad for sqlserver5.7

    toad for sqlserver5.7 虽然SSMS很好很强大,不过有时候使用一些第三方工具可以使MSSQL DBA们更加的方便管理MSSQL toad for sqlserver5.7就是这样一 ...

  3. [原]用WebBrowser组件模拟人工运行搜索引擎自动点击搜索结果的实验

    本代码只是业余时间无聊写着试试,用WebBrowser组件模拟人工运行搜索引擎自动点击搜索结果的实验 这是网络中盛传的提高搜索引擎点击率的一种方式,当然属于作弊,不推荐各位使用.另外这种方式的性能不佳 ...

  4. Linq 时间对比陷阱坑

    同样的两个datetime 格式的时间     2013年12月2日 17点29分57秒  

  5. JAVA安卓和C# 3DES加密解密的兼容性问题(2013年8月修改版)

    近 一个项目.net 要调用JAVA的WEB SERVICE,数据采用3DES加密,涉及到两种语言3DES一致性的问题, 下面分享一下, 这里的KEY采用Base64编码,便用分发,因为Java的By ...

  6. 修改placeholder属性

    input::-webkit-input-placeholder{ font-size:12px;}input:-ms-input-placeholder{ font-size:12px;}input ...

  7. MSSQL 2005数据库与SP4补丁安装

    Sql Server 2005 正确安装之前的win7配置: http://wenku.baidu.com/link?url=6T3jzVnu2XY_sfqfe9ZqQ_6dUOdrZwHc83baW ...

  8. Redis,Memcache,mongoDB的区别

    从以下几个维度,对redis.memcache.mongoDB 做了对比,欢迎拍砖 1.性能 都比较高,性能对我们来说应该都不是瓶颈 总体来讲,TPS方面redis和memcache差不多,要大于mo ...

  9. SQL删除数据库里所有表的外键,同时删除所有用户表

    SQL删除数据库里所有表的外键,同时删除所有用户表 删除所有的用户表的外键,直接将下面的代码拷贝到数据库里执行即可: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  10. HDU5772 String problem 最大权闭合图+巧妙建图

    题意:自己看吧(不是很好说) 分析: 网络流:最大权闭合子图. 思路如下: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得 ...