D. Register, fedorandalexareinclutiveguys. Nowtheyinventthegamewithstringsfortwoplayers. Givenagroup
D. 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 valid tive guys. now they invent the game with strings for two players. given a group
D. 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 still tive guys. Now they invent the game with strings for two players.
Given a groupNNon-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 gameKTimes. The player who is the loser ofI-Th game makes the first move in (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,NAndK(1? ≤?N? ≤? 105; 1? ≤?K? ≤? 109 ).
Each of the nextNLines 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 3ab
Output
First
Input
3 1abc
Output
First
Input
1 2ab
Output
Second
Question:
Give N characters and follow the rules (it is too long to translate here ). Then, the person who loses in the I-th segment can start in the I + 1-th segment and now asks who wins in the k-th segment.
Solution:
First, we store these characters and use trie to store them. With trie, we can easily think of tree-type DP. The DP here is not the optimal value, but the result of winning or losing a node.
We first set the node v, and win [v] indicates that the characters that have been assembled match exactly v. Then, we need to perform the next match to determine whether the first hand can win, lose [v] indicates whether the first hand will lose.
Leaf node, win [v] = false, lose [v] = true.
Other node win [v] = win [v] |! Win [child], lose [v] = lose [v] |! Lose [child ).
If win [0] = true, lose [0] = true, it means that people in the first game can manipulate the result. Otherwise, the number of k will determine whether the result can win.
Code:
#include
#include
#define N_max 123456#define sigma_size 26using namespace std;bool win[N_max], lose[N_max];int n, k;char st1[N_max];class Trie{public:int ch[N_max][sigma_size];int sz;Trie() {sz=0;memset(ch[0], 0, sizeof(ch[0]));}int idx(char c) {return c-'a';}void insert(char *s) {int l = strlen(s), u = 0;for (int i = 0; i < l; i++) {int c = idx(s[i]);if (!ch[u][c]) {ch[u][c] = ++sz;memset(ch[sz], 0, sizeof(ch[sz]));}u = ch[u][c];}}};Trie T;void init() {scanf("%d%d", &n, &k);for (int i = 1; i <= n; i++) {scanf("%s", st1);T.insert(st1);}}void dfs(int v) {bool is_leaf = true;win[v] = false;lose[v] = false;for (int i = 0; i < sigma_size; i++) {int tmp = T.ch[v][i];if (tmp) {is_leaf = false;dfs(T.ch[v][i]);win[v] |= !win[T.ch[v][i]];lose[v] |= !lose[T.ch[v][i]];}}if (is_leaf) {win[v] = false;lose[v] = true;}}void ans(bool res) {puts(res? "First":"Second");}void solve() {dfs(0);if (win[0] && lose[0])ans(true);else if (win[0])ans(k&1);elseans(0);}int main() {init();solve();}