CodeChef A String Game(SG)

A String Game

 
Problem code: ASTRGAME
 

All submissions for this problem are available.

Teddy and Tracy like to play a game based on strings. The game is as follows. Initially, Tracy writes a long random string on a whiteboard. Then, each player starting with Teddy makes turn alternately. Each turn, the player must erase a contiguous substring that exists in the dictionary. The dictionary consists of N words.

Of course, the player that can't erase any substring in his turn loses the game, and the other player is declared the winner.

Note that after a substring R is erased, the remaining substring becomes separated, i.e. they cannot erase a word that occurs partially to the left of R and partially to the right of R.

Determine the winner of the game, assuming that both players play optimally.

Input

The first line contains a single integer T, the number of test cases. T test cases follow. The first line of each testcase contains a string S, the string Tracy writes on the whiteboard. The next line contains a single integer N. N lines follow. The i-th line contains a single string wi, the i-th word in the dictionary.

Output

For each test case, output a single line containing the name of the winner of the game.

Example

Input:
3
codechef
2
code
chef
foo
1
bar
mississippi
4
ssissi
mippi
mi
ppi

Output:
Tracy
Tracy
Teddy

Constraints

  • 1 <= T <= 5
  • 1 <= N <= 30
  • 1 <= |S| <= 30
  • 1 <= |wi| <= 30
  • S and wi contain only characters 'a'-'z'

 

SG博弈

 

#include <bits/stdc++.h>
using namespace std ;
const int N = 33 ;
bool check[N][N] ;
int sg[N][N] , vis[10010] ;
string s , word ;
int main() {
//    freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    int _ , n ; cin >> _ ;
    while( _-- ) {
        cin >> s ; int slen = s.length() ;
        memset( check , false , sizeof check );
        memset( vis , 0 , sizeof vis);
        memset( sg , 0 , sizeof sg );
        cin >> n ;
        for( int i = 0 ; i < n ; ++i ) {
            cin >> word ; int wlen = word.length() ;
            for( int j = 0 ; j + wlen <= s.length() ; ++j ) {
                if( word == s.substr( j , wlen ) )
                    check[j][j+wlen] = true ;
            }
        }
        int cnt = 0;
        for( int len = 1 ; len <= slen ; ++len ) {
            for( int be = 0 ; be < slen ; ++be ) {
                cnt++ ; int ed = be + len ; if( ed > slen ) continue ;
                for( int i = be ; i < ed ; ++i ){
                    for( int j = i + 1 ; j <= ed ; ++j ) if( check[i][j] ){
                        vis[ sg[be][i]^sg[j][ed] ] = cnt ;
                    }
                }
                int z = 0 ;
                while( vis[z] == cnt ) z++;
                sg[be][ed] = z ;
            }
        }
        if( sg[0][slen] ) cout << "Teddy" << endl ;
        else cout << "Tracy" << endl ;

    }
}
View Code 

转载于:https://www.cnblogs.com/hlmark/p/4304744.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值