Codeforces 455b a lot of games (Dictionary tree + game)

Source: Internet
Author: User

Question connection: codeforces 455b a lot of games

Given n represents a string set. Given K, the game is played K times, followed by N strings. Starting from each board, the string is an empty string, and then the two append characters at the end in turn to ensure that the new string is the prefix of a string in the set and cannot be input by the operator, the new round begins with the previous sentence.

Solution: first, build a dictionary tree for the character set, and then search for the first-hand decision-making status based on the game's winning and losing nature. You can decide to win 3, only win 2, and only fail 1, uncontrollable (that is, the opponent can decide to win) 0.
For the status 3, to win, you can use the former K-1 field defeat, and then ensure that the K field first hand, take the winning solution.
For status 2, if both of them win, it must be that two people take turns to win, so K's parity is determined.
For statuses 1 and 0, it is necessary to lose because you keep losing and keep starting first.

#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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.