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. Centos7 配置rsyslog客户端接收远程日志

    rsyslog 因为路由器我设定每天重启,但是日志一重启就会清除,并且路由器最多只能保存1024条记录,所以我想把路由器的日志记录到一台服务器上,发现路由器包含远程日志功能 于是我就在我的centos ...

  2. js url传参,参数加密

    前台 function encode64(input) { var output = ""; var base = new Base64(); var output = base. ...

  3. Uboot命令U_BOOT_CMD分析

    其中U_BOOT_CMD命令格式如下: U_BOOT_CMD(name,maxargs,repeatable,command,"usage","help") 各 ...

  4. [python 学习] 类

    #!/usr/bin/python # -*- encoding:utf-8 -*- class Animal: animal_num = 0 class Dog(Animal): #类帮助文档 't ...

  5. Flutter-charts_flutter圖表

    pub.dev搜索charts_flutter 導入依賴 charts_flutter: ^0.8.1 項目導入 import 'package:charts_flutter/flutter.dart ...

  6. django之创建项目

    1.创建虚拟环境 mkvirtualenv django_study -p python3 创建成功后:(django_study) python@ubuntu:~$ 2.安装django-指定版本1 ...

  7. 22.Express框架——2019年12月19日

    2019年12月19日14:16:36 1. express简介 1.1 介绍 Express框架是后台的Node框架,所以和jQuery.zepto.yui.bootstrap都不一个东西. Exp ...

  8. web框架之初识Django

    目录 一.web框架 1.1什么是web框架 1.2自制的简易web框架 1.3三大主流web框架简介 Django Flask Tornado 1.4动态网页与静态网页 二.初识Django框架 2 ...

  9. 【leetcode】1027. Longest Arithmetic Sequence

    题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...

  10. DOSUtil

    package Testlink; import java.io.BufferedReader; import java.io.File; import java.io.IOException; im ...