Sicily 1900. Word Game

来源:互联网 发布:电脑多开软件 编辑:程序博客网 时间:2024/06/02 06:42

1900. Word Game

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

It’s a game with two players. Given a dictionary of words, a word S chosen from the dictionary to start with, and a word T also chosen from the dictionary as the winning word, which will be described below, the two players take turns to choose a word from the dictionary, satisfying that the first letter of the chosen word is the same as the last letter of previous word. Each word could be chosen more than once.
Suppose they play exactly n rounds. At the last round, if the player (the first one if n is odd, the second one otherwise) chooses the winning word T, he wins. To your surprise that, the two players are not so clever that they choose words randomly.
Here comes the question. How many different ways will the first player win if they play no more than N rounds, among all the possible ways satisfying all the conditions above? 

Input

An integer C, indicates the number of test cases.
Then comes C blocks, formatted like this:
An integer M, indicates the number of words in the dictionary, M <= 50.
M string consisting of only lowercase letters, represent the words in the dictionary. The length of each word is no more than 10. There are no duplicated words.
String S, the word to start with.
String T, the winning word.
A positive integer N, indicates the maximum number of rounds to play. N fits in a signed 32-bit integer. 

Output

For each test case, print a line with an integer W, indicating how many possible ways the first player wins.
Output the answer modulo 10001. 

Sample Input

13abccaccdeabccde3

Sample Output

2

// Problem#: 1900// Submission#: 3590598// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <string.h>int last(char s[]) {    return s[strlen(s) - 1] - 'a';}void mul(int a[52][52], int x[52][52]) {    int t[52][52];    int i, j, k;    memset(t, 0, sizeof(t));    for (i = 0; i < 52; i++)        for (j = 0; j < 52; j++)            for (k = 0; k < 52; k++)                t[i][j] = (t[i][j] + a[i][k] * x[k][j]) % 10001;    memcpy(a, t, sizeof(t));}int main() {    int m, n, b[52][52], x[52][52], i, j, cs;    char s[11];    scanf("%d", &cs);    while (cs--) {        memset(b, 0, sizeof(b));        memset(x, 0, sizeof(x));        scanf("%d", &m);        while (m--) {            scanf("%s", s);            x[s[0] - 'a'][last(s)]++;        }        scanf("%s", s);        int vs = last(s);        scanf("%s", s);        int vt = s[0] - 'a';        scanf("%d", &n);        mul(x, x);        for (i = 0; i < 26; i++) {            x[i + 26][i] = 1;            x[i + 26][i + 26] = 1;            b[i][i] = 1;            b[i][i + 26] = 1;        }        int p = (n - 1) / 2;        for (i = 0; (1 << i) <= p; i++) {            if (p & (1 << i)) mul(b, x);            mul(x, x);        }        printf("%d\n", b[vs][vt]);    }    return 0;}                                 


0 0