CodeChef A String Game(SG)
2015-02-28 09:50
411 查看
A String GameProblem code: ASTRGAME | Submit All Submissions |
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 <= 51 <= 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 ; int sg , 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
相关文章推荐
- CodeChef November Lunchtime 2013 Lucy and the Number Game(简单题)
- 字典树 Robot Game CodeChef - TR2
- Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)E. Game with String(枚举)
- codeforces E. Game with String 概率
- Codeforces Round #402 (Div. 1) A. String Game
- codechef The Lead Game 题解
- ●CodeChef Sereja and Game
- [猜结论][乱搞][博弈]Codechef . Palindromic Game
- codechef Subtraction Game 1题解
- CodeChef Sereja and Game [DP 概率 博弈论]
- CodeChef:Girl Friend and String Gift(dp)
- C - A Game of Balls CodeChef - GAMEBALL 思维模拟
- CodeChef STRMRG String Merging (dp)
- codechef ANKGAME
- Codeforces Round #426 (Div. 2)The Meaningless Game【数学题】【水题】
- lintcode Guess Number Game II
- CodeChef TechFest 2013 Balancing nature(暴力)
- 【 Codeforces Round #430 (Div. 2) A 】 Kirill And The Game
- STL std:string引起的User breakpoint exception/User breakpoint called from code at
- Codeforces Round #334 E. Lieges of Legendre(SG)