Codeforces 455B A Lot of Games(字典樹+博弈)

來源:互聯網
上載者:User

標籤:style   http   color   os   io   for   ar   line   

題目串連: Codeforces 455B A Lot of Games

題目大意:給定n,表示字串集合。給定k,表示進行了k次遊戲,然後是n個字串。每局開始,字串為空白串,然後兩人輪流在末尾追加字元,保證新的字串為集合中某字串的首碼,不能操作者輸,新一輪由上一句輸的人先手。

解題思路:首先對字元集合建立字典樹,然後根據博弈的必勝必敗性質搜尋出先手的決策狀態,可決定勝敗3,只能勝利2,只能失敗1,不可掌控(即對手可決定勝敗)0。
對於狀態3,為必勝,可以採用前K-1場敗,然後保證第K場自己先手,取必勝方案。
對於狀態2,無論則們走都是贏的話,肯定是兩個人輪流勝局,所以判斷K的奇偶性。
對於狀態1和0,必敗,因為一直輸,一直先手。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1e5+5;struct node {    int val, arr[30];    node () {        val = 0;        memset(arr, 0, sizeof(arr));    }}t[maxn*2];int top = 1, len, n, k;char str[maxn];inline int get_node () {    return top++;}void insert (int x, int d) {    if (d == len)        return;    int mv = str[d] - ‘a‘;    if (t[x].arr[mv] == 0)        t[x].arr[mv] = get_node();    insert(t[x].arr[mv], d+1);}int solve (int x) {    int& ans = t[x].val;    ans = 0;    bool flag = true;    for (int i = 0; i < 26; i++) {        if (t[x].arr[i]) {            flag = false;            ans |= solve(t[x].arr[i]);        }    }    if (flag)        ans = 1;    return 3-ans;}int main () {    scanf("%d%d", &n, &k);    for (int i = 0; i < n; i++) {        scanf("%s", str);        len = strlen(str);        insert(0, 0);    }    solve(0);    int ans = t[0].val;    if (ans < 2)        printf("Second\n");    else if (ans == 2)        printf("%s\n", k&1 ? "First" : "Second");    else        printf("First\n");    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.