Codeforces #260 (Div. 1) B. A Lot of Games(数据结构:trie+DFS)

这篇博客讨论了一种两个人玩的字符串构造游戏,玩家必须在给定字符串集合的前缀下进行移动。游戏进行多次,最后一局的胜者是总赢家。文章介绍了解决策略,通过构建Trie树并进行DFS来确定先手是否必胜。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. A Lot of Games
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players.

Given a group of n non-empty strings. During the game two players build the word together, initially the word is empty. The players move in turns. On his step player must add a single letter in the end of the word, the resulting word must be prefix of at least one string from the group. A player loses if he cannot move.

Andrew and Alex decided to play this game k times. The player who is the loser of the i-th game makes the first move in the (i + 1)-th game. Guys decided that the winner of all games is the player who wins the last (k-th) game. Andrew and Alex already started the game. Fedor wants to know who wins the game if both players will play optimally. Help him.

Input

The first line contains two integers, n and k (1 ≤ n ≤ 1051 ≤ k ≤ 109).

Each of the next n lines contains a single non-empty string from the given group. The total length of all strings from the group doesn't exceed 105. Each string of the group consists only of lowercase English letters.

Output

If the player who moves first wins, print "First", otherwise print "Second" (without the quotes).

Sample test(s)
input
2 3
a
b
output
First
input
3 1
a
b
c
output
First
input
1 2
ab
output
Second
题意是给定n个字符串和k,表示进行k场游戏

每场游戏从一个空字符串开始,每次添加一个字符保证添加之后得到的字符串至少为给出字符串其中一个的前缀

当前这场游戏输的人下一场先开始

我觉得这个题光是理解就很蛋疼...

看了好多人的答案好像都是用trie建前缀树

ans[i][0] = 1表示选定前缀树中i字符的人会输

ans[i][1] = 1表示选定前缀树中i字符的人会赢

建树完成后从下到上dfs即可

代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#define MAXN 100100
#define LL long long
using namespace std;

//ans[i][0] = 1表示当前在i处的人会输
//ans[i][1] = 1表示当前在i处的人会赢
int cnt = 1;
int vis[MAXN][30];
int ans[MAXN][2];

void DFS(int st) {
    bool b = 0;
    for(int i=0; i<30; ++i) {
        if(vis[st][i]) {
            b = 1;
            int nxt = vis[st][i];
            DFS(nxt);
            ans[st][0] |= !ans[nxt][0];
            ans[st][1] |= !ans[nxt][1];
        }
    }
    ans[st][1] |= !b;
    return ;
}

int add(int st, int ch) {
    int &cur = vis[st][ch];
    return cur ? cur : cur = cnt++;
}

int main(void) {
    int n, k, st;
    string str;
    cin >> n >> k;
    while(n--) {
        cin >> str;
        st = 0;
        for(int i=0; i<str.size(); ++i) {
            st = add(st, str[i]-'a');
        }
    }

    DFS(0);

    if(!ans[0][0] || (!ans[0][1]&&k%2==0))
        cout << "Second" << endl;
    else cout << "First" << endl;
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值