Codeforces 176B Word Cut (Count DP)

Source: Internet
Author: User

Word CutTime limit:2000MS Memory Limit:262144KB 64bit IO Format:%i64d &%i6 4u SubmitStatusPracticecodeforces 176B

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 a 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 Words  start  and  end . Count in how many ways we can transform Word  start  into word  end , if we apply Exactly K split  operations consecutively to Word  start .

Ways is considered different if the sequences of applied operations differ. Operation sequences is different if exists such number i (1≤ ik), that in the i-th operation of the first sequence the word splits into parts x and y, in the c11>i-th operation of the second sequence the word splits into parts a and b, a nd additionally xa holds.

Input

The first line contains a non-empty word start, the second line contains a non-empty word end. T He words consist of lowercase Latin letters. The number of letters in Word start equals the number of letters in Word end and are at least 2 and doesn ' t exceed letters.

The third line contains integer k (0≤ k ≤105)-the required number of operations.< /c14>

Output

Print a single number-the answer to the problem. As this number can be rather large, print it modulo 1000000007(9 + 7).

Sample Input

Input
Ab
Ab
2
Output
1
Input
Ababab
Ababab
1
Output
2
Input
Ab
Ba
2
Output
0

Hint

The sought-in-the-first sample is:

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

In the second sample the sought ways is:

    • Ababab → Abab|ab → ababab
    • Ababab → Ab|abab → ababab

"Test Instructions":

Divide a string:w = xy into u = yx(whole order changed, content unchanged)

Ask how many ways, so that just after the K-time division, from the original string into a destination string.

"Problem-solving ideas":

Compare the typical count DP.

Transfer process: (after I divide, how many ways to this string)

Start: Number of original string = 1; other string mode = 0

First: The original string =0 (because it cannot become itself); other =1 (from the original string from the beginning)

Second time: The original string =n-1 (all other strings can be changed back); other =n-2 (except the other + original strings)

......

The DP state should be divided by the number of times K and start point I

But using a scrolling array can divide the number of times

Codeforces 176B Word Cut (Count DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.