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. MySQL的删除语句

    虽然现在数据库空间越来越大,但处理数据时候还是有要删除的时候,以下整理了一些最常用的删除语句. 分成两种 一个是删除指定数据,另一个删除所有数据. 一.删除指定数据 DELETE FROM 表名 WH ...

  2. Katalon Recorder初探

    缘由 最近工作中,有大量web页面上的重复操作,实在麻烦,就想利用selenium ide录制出一些操作集合,以便复用.新版本的selenium ide 3目前只支持firefox最新的版本,而且代码 ...

  3. 原生JS实现图片循环切换

    <!-- <!DOCTYPE html> <html> <head> <title>原生JS实现图片循环切换 —— 方法一</title&g ...

  4. 雷哥带你了解webSocket原理

    偶然在知乎上看到一篇回帖,瞬间觉得之前看的那么多资料都不及这一篇回帖让我对 websocket 的认识深刻有木有.所以转到我博客里,分享一下.比较喜欢看这种博客,读起来很轻松,不枯燥,没有布道师的阵仗 ...

  5. OC + RAC (四) combineLatest和merg

    -(void)_test4{ ///RAC combineLatest和merge // combineLatest只有当两个信号都发送了 订阅者才能收到信息 结果一次收到 结果是数组 // merg ...

  6. django的安装和初步使用

    安装参考:步骤也可以参考这个 很详细 https://blog.csdn.net/zww1984774346/article/details/54408759 如果想在终端查看项目结构 需要用到tre ...

  7. 6,Stack

    一,Stack简介 Stack是栈.它的特性是:先进后出(FILO, First In Last Out). java工具包中的Stack是继承于Vector(矢量队列)的,由于Vector是通过数组 ...

  8. [CF959A]Mahmoud and Ehab and the even-odd game题解

    题意简述 一个数n,Mahmoud珂以取(即如果取\(k\),使\(n = n - k\))一个正偶数,Ehab珂以取一个正奇数,一个人如果不能取了(对于Mahmoud和Ehab \(n = 0\), ...

  9. 安卓环境home assistant搭建

    准备搞个智能家居玩玩 先从home assistant(后面简写为HASS)开始吧 莫得树莓派,拿旧手机凑活一下 准备材料: root过的安卓机 一.安卓机Linux环境搭建 个人习惯不详细写基础环境 ...

  10. .net core跨平台

    https://www.cnblogs.com/artech/p/7812811.html .net简介:https://baike.baidu.com/item/.NET/156737?fr=ala ...