zoukankan      html  css  js  c++  java
  • CodeChef A String Game(SG)

    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 = 33 ;
    bool check[N][N] ;
    int sg[N][N] , 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 
  • 相关阅读:
    在github上面查到了,为什么需要这个padding
    Java 8 新语法习惯,新的函数特性和语法
    数据库连接池配置,获取连接的超时
    用函数式的方式思考,通常以命令式的方式
    centos7 yum方式安装,centos自带mariadb
    mac屏幕脏了怎么办?避免使用粗糙的布
    用 Arthas “庖丁解牛,强大的 Arthas法师来 carry
    图解Knative核心组件,Serving自动伸缩
    vim配置文件
    20200717模拟赛3题解
  • 原文地址:https://www.cnblogs.com/hlmark/p/4304744.html
Copyright © 2011-2022 走看看