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
ab
ab
2
1
ababab
ababab
1
2
ab
ba
2
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 ;
}