CodeForces 176B Word Cut(DP)

题意:给你a串和b串,你能切k次,每次切完将尾部分放在头的前面,问有多少种方案切k次从a串变为b串

思路:令dp[i][0]为砍了i次变成b串的方案数,dp[i][1]为砍了i次变成非b串的方案数,然后预处理一下前缀就可以DP了


#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
#define LL long long
LL dp[300000][2];
int main()
{
	string a,b;
	int k;
	cin >> a >> b >>k;
	int lena = a.size();
	int lenb = b.size();
	if (a==b)
		dp[0][0]=1;
	else dp[0][1]=1;
	int x = 0;
	for (int i = 0;i<lena;i++)
	{
		int flag = 1;
		for (int j = 0;j<lena;j++)
		{
			if (a[(i+j)%lena] != 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]=((lena-x)*dp[i][0]+(lena-x-1)*dp[i][1])%mod;
	}
	printf("%d\n",dp[k][0]);
}

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

转载于:https://www.cnblogs.com/q934098774/p/5388694.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值