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 = ;
bool check[N][N] ;
int sg[N][N] , vis[] ;
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 , , sizeof vis);
memset( sg , , sizeof sg );
cin >> n ;
for( int i = ; i < n ; ++i ) {
cin >> word ; int wlen = word.length() ;
for( int j = ; j + wlen <= s.length() ; ++j ) {
if( word == s.substr( j , wlen ) )
check[j][j+wlen] = true ;
}
}
int cnt = ;
for( int len = ; len <= slen ; ++len ) {
for( int be = ; be < slen ; ++be ) {
cnt++ ; int ed = be + len ; if( ed > slen ) continue ;
for( int i = be ; i < ed ; ++i ){
for( int j = i + ; j <= ed ; ++j ) if( check[i][j] ){
vis[ sg[be][i]^sg[j][ed] ] = cnt ;
}
}
int z = ;
while( vis[z] == cnt ) z++;
sg[be][ed] = z ;
}
}
if( sg[][slen] ) cout << "Teddy" << endl ;
else cout << "Tracy" << endl ; }
}

最新文章

  1. win7升级为Win10 10586版本,出现应用商店打不开的解决办法
  2. 关于iphone消息推送把C#当服务器端来发送
  3. hdu 1002 java 大数相加
  4. [置顶] 很荣幸被选为2013年度 CSDN博客之星评选,如果觉得我的文章可以,请投我一票!
  5. Android(java)学习笔记112:局部位置的内部类的介绍
  6. 区间求mex的两种方法
  7. 武汉科技大学ACM:1010: 零起点学算法27——判断是否直角三角形
  8. iOS之UITableView带滑动操作菜单的Cell
  9. Kotlin——最详解的类(class)的使用
  10. 使用Dagger2做静态注入, 对比Guice.
  11. javascript获取系统时间
  12. Docker学习笔记 - Docker的镜像
  13. 聊聊javaMail
  14. javascript中Ajax的简单封装
  15. 在网页中运用统计Web Service接口
  16. android AIDL 语言用法
  17. 【转】jumpserver 堡垒机环境搭建(图文详解)
  18. xml 转 数组
  19. Tools - Others
  20. 20145333茹翔 Exp5 MS11_050

热门文章

  1. RSA加密原理与秘钥、公钥生成
  2. 11.SUSE Linux服务器系统网卡配置重启问题
  3. nginx的RPM包制作案例
  4. K8S进入容器方法
  5. 对getBoundingClientRect属性的研究
  6. 当 Messaging 遇上 Jepsen
  7. 微信公众号号开发(Java)
  8. Java后端进阶教程
  9. C#仿QQ皮肤-Label与ListBox 控件实现----寻求滚动条的解决方案
  10. 201903-2 CCF 二十四点