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

CodeChef A String Game(SG)的更多相关文章

  1. hdu 1809 求SG函数

    A New Tetris Game(2) Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. 微信公众号开发(一)--验证服务器地址的Java实现

    现在主流上都用php写微信公众号后台,其实作为后端语言之一的java也可以实现. 这篇文章将对验证服务器地址这一步做出实现. 参考资料:1.慕课网-<初识java微信公众号开发>,2.微信 ...

  3. 《用delphi开发共享软件》-15.2桌面提示器

    打开一个配置文件: 打开一个配置文件 操作TStringGrid Procedure EmptyGrid(Var sg:TStringGrid); Var i:Integer; begin do sg ...

  4. Java SE 8 流库

    1. 流的作用 通过使用流,说明想要完成什么任务,而不是说明如何去实现它,将操作的调度留给具体实现去解决: 实例:假如我们想要计算某个属性的平均值,那么我们就可以指定数据源和属性,然后,流库就可以对计 ...

  5. 通过Visualizing Representations来理解Deep Learning、Neural network、以及输入样本自身的高维空间结构

    catalogue . 引言 . Neural Networks Transform Space - 神经网络内部的空间结构 . Understand the data itself by visua ...

  6. Java SE 8 流库(一)

    1. 流的作用 通过使用流,说明想要完成什么任务,而不是说明如何去实现它,将操作的调度留给具体实现去解决: 实例:假如我们想要计算某个属性的平均值,那么我们就可以指定数据源和属性,然后,流库就可以对计 ...

  7. 把list(对象)集合中的(某个属性),放到数组中。

    List<SpecialguardInfo> list=specialguardOrderService.findfreeSg(date1,date2);//得到list对象集合 Stri ...

  8. 2019的hdu暑假作业(欢迎纠错)

    1219 遍历计数. #include<bits/stdc++.h> #define QAQ 0 using namespace std; ]; ]; int main(){ )){ me ...

  9. java - day019 - 反射

    网络程序,难点在线程 反射 reflect 实用 类对象 来执行反射操作 反射获得一个类的定义信息 反射创建对象 反射调用成员变量, 方法 方法 获得类对象的三种方式 A.class Class.fo ...

随机推荐

  1. 查看Xcode里的描述文件

    iOS应用打包离不开描述文件,也就是mobileprovision文件. 一般我们的操作是双击,Xcode就会运行该文件.但是具体文件里是什么,Xcode又是否真的加载了该文件?文件里又描述了什么呢? ...

  2. scapy - 基于python的数据包操作库

    简介 地址:https://github.com/secdev/scapy scapy是一个基于python的交互式数据包操作程序和库. 它能够伪造或者解码多种协议的数据包,通过使用pcap文件对他们 ...

  3. Windows系统中,循环运行.bat/.exe等文件

    一.创建循环运行的run-everySecond.vbs文件[双击次文件即可启动运行] dim a set a=CreateObject("Wscript.Shell") Do # ...

  4. Test 6.24 T1 购物

    问题描述 小 C 今天出去购物,商店里总共有 n 种商品,小 C 的钱够买至多 k 个商品. 小 C 对每种商品都有一个喜爱程度,但如果买了同一种商品很多次,小 C 对这种商品的喜爱程度就会降低. 具 ...

  5. Queue2链队列

    链队列 1 #include <iostream> using namespace std; template <class T> class Queue { private: ...

  6. Favorite Donut

    Favorite Donut Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  7. 关于C(n,m) 的奇偶 ,与C(n,0),C(n,1),C(n,2)…C(n,n).当中有多少个奇数

    (n & m) == m  为奇数 C(n,0),C(n,1),C(n,2)…C(n,n).当中有多少个奇数 第一种想法是Lucas定理推导,我们分析一下 C(n,m)%2,那么由lucas定 ...

  8. Sending form data

    https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_and_retrieving_form_data This arti ...

  9. gradle命令行打包不同环境的apk包

    为什么我搜这个? 因为 早上在开发那边我看到他控制不同环境就是在这个下拉框选择不同的环境的 搜索词: Gradle 入门--只此一篇 https://www.jianshu.com/p/001abe1 ...

  10. Jenkins使用四:Jenkins创建任务,实现代码有改动时,自动构建

    新建任务 指定在哪台节点运行 添加要监控的git地址和使用账号,此账号为设置节点时配置公私钥时设置的ssh登录账号 设置检查代码是否有变更的频率,每三分钟检查一次,如果检查到有变更就构建 修改文件再提 ...