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

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

Input

abab2

Output

1

Input

abababababab1

Output

2

Input

abba2

Output

0

Sample Output

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

题目大意就是问有几种操作方法,能在k次操作下得到目标串。

首先要肯定的是题目给的操作相当于一个循环串的位移。(位移不为0)

这样的话,就可以对目标串的第一位分析了。

记s(now, k)表示经过k次操作后,目标串的第一位在当前串的第now位。(初始k == 0时,由于串是循环串,会出现不止一个now的值不为0)

于是s(now, k) = sum{s(i, k-1)} (i != now)

只要不是本身就在now,都能位移到now。

大一时做这个题,到就只能想到这里,然后进行了O(k*strlen(str)*strlen(str))的暴力运算。

但是上述式子可以进行一定的变形:

s(now, k) = sum - s(now, k-1)

这样的话,只需要记录下sum,就能在O(k*strlen(str))的时间复杂度下完成了。

需要注意的是,由于采用一维dp会导致计算s(now)的时候破坏了前一个状态。

所以此处需要同时保存前一个状态和当前状态。所以开了第二维,且第二维仅保留两次的状态。(此处采用了亦或运算进行优化)

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define N 1000000007

using namespace std;

char from[1005], to[1005];
int n, len, s[1005][2], sum;

bool Pair(int now)
{
    int i = 0;
    for (;;)
    {
        if (i == len)
            return true;
        if (from[now] != to[i])
            return false;
        i++;
        now = (now+1) % len;
    }
}

bool Input()
{
    if (scanf("%s", from) == EOF)
        return false;
    scanf("%s%d", to, &n);
    memset(s, 0, sizeof(s));
    len = strlen(from);
    sum = 0;
    for (int i = 0; i < len; ++i)
    {
        if (Pair(i))
        {
            s[i][0] = 1;
            sum++;
        }
    }
    return true;
}

void Work()
{
    int tem, state = 0;
    for (int i = 1; i <= n; i++)
    {
        tem = 0;
        state = state^1;
        for (int j = 0; j < len; ++j)
        {
            s[j][state] = sum - s[j][state^1];
            s[j][state] = (s[j][state]%N+N)%N;
            tem += s[j][state];
            tem %= N;
        }
        sum = tem;
    }
    printf("%d\n", s[0][state]);
}

int main()
{
    //freopen("test.in", "r", stdin);
    while (Input())
    {
        Work();
    }
    return 0;
}
时间: 2024-10-22 17:52:24

ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)的相关文章

CodeForces 176B Word Cut (计数DP)

Word Cut Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 176B Description Let's consider one interesting word game. In this game you should transform one word into another through specia

ACM学习历程—HDU1028 Ignatius and the Princess III(递推 || 母函数)

Description "Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says. "The second problem is, given an positive integer N, we define an equation like this:   N=a[1]+a[2]+a[3]+...+a[m];   a

ACM学习历程—CodeForces 601A The Two Routes(最短路)

题目链接:http://codeforces.com/problemset/problem/601/A 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没有陆路. 这种情况下就会有一个结论,就是至少有一种交通可以直接1到n. 这样只需要对另一种跑一个最短路就OK了. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath&

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

ACM学习历程—UESTC 1226 Huatuo&#39;s Medicine(数学)(2015CCPC L)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #

ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

Description We all use cell phone today. And we must be familiar with the intelligent English input method on the cell phone. To be specific, the number buttons may correspond to some English letters respectively, as shown below: 2 : a, b, c    3 : d

ACM学习历程—BestCoder Round #75

1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include &l

ACM学习历程—HDU2068 RPG的错排(组合数学)

Description 今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁.RPG给他机会让他猜猜,第一次猜:R是公主,P是草儿,G是月野兔:第二次猜:R是草儿,P是月野兔,G是公主:第三次猜:R是草儿,P是公主,G是月野兔:......可怜的野骆驼第六次终于把RPG分清楚了.由于RPG的带动,做ACM的女生越来越多,我们的野骆驼想都知道她们,可现在有N多人,他要猜的次数可就多了,为了不为难野骆驼,女生们只要求他答对一半或以