Sicily 1900. Word Game

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

1
3
abc
cac
cde
abc
cde
3

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;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值