1. 程式人生 > >Codeforces Gym 100814J Game 簡單博弈

Codeforces Gym 100814J Game 簡單博弈

題目大意:

就是現在初始的時候有一個字串S, 長度不超過10000

兩個人輪流操作

每次可以選擇從左向右將連續兩個字元按照給出的矩陣進行變化

或者選擇從右往左

如果最後剩下的是母音字母則Salah獲勝, 否則Marzo獲勝

Salah先手問誰會獲勝

大致思路:

比較水, 考慮到S在每次變換之後長度都會接近減半, 於是可以直接dfs暴力搜尋

記下當前串以及當前輪到誰即可

程式碼如下:

Result  :  Accepted     Memory  :  148 KB     Time  :  31 ms

/*
 * Author: Gatevin
 * Created Time:  2015/11/21 16:25:41
 * File Name: Sakura_Chiyo.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

int T;
char s[30][30];

char get(char c1, char c2)
{
    return s[c1 - 'a'][c2 - 'a'];
}

bool isVowel(char c)
{
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

int dfs(string now, char who)
{
    int sz = now.size();
    if(sz == 1 && isVowel(now[0]))
        return 1;
    if(sz == 1 && !isVowel(now[0]))
        return 0;
    string left , right;
    for(int i = 0; i + 1 < sz; i += 2)
        left.push_back(get(now[i], now[i + 1]));
    if(sz & 1) left.push_back(now[sz - 1]), right.push_back(now[0]);
    for(int i = (sz & 1); i < sz; i += 2)
        right.push_back(get(now[i], now[i + 1]));
    int L = dfs(left, who == 's' ? 'm' : 's'), R = dfs(right, who == 's' ? 'm' : 's');
    if(who == 's')
    {
        if(L == 1 || R == 1) return 1;
        return 0;
    }
    else
    {
        if(L == 0 || R == 0) return 0;
        return 1;
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin>>T;    
    while(T--)
    {
        string tmp;
        for(int i = 0; i < 26; i++)
        {
            cin>>tmp;
            for(int j = 0; j < 26; j++)
                s[i][j] = tmp[j];
        }
        string in;
        cin>>in;
        printf("%s\n", dfs(in, 's') ? "Salah" : "Marzo");
    }
    return 0;
}
/*

1
daeaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
eaaaaaaaaaaaaaaaaaaaaaaaaa
aaaahaaaaaaaaaaaaaaaaaaaaa
aaahaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
acaa

*/