程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> codeforces Round #260(div2) D解題報告

codeforces Round #260(div2) D解題報告

編輯:C++入門知識

codeforces Round #260(div2) D解題報告


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 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?≤?105; 1?≤?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個字符,按照規則玩(太長了,這裡就不翻譯了)。然後第i-th輸了的人,可以在第i+1-th先手,現在要求第k-th贏了的人是誰。

解法:

首先存這些字符,用trie來存,通過trie就很容易聯想到樹型DP,這裡的DP就不是取最優值之類的了,而是用來弄到達某個節點的勝負情況。

我們首先設節點v,win[v]代表已經組裝好的字符剛好匹配到v了,然後需要進行下一步匹配時,先手是否可以贏,lose[v]則代表先手是否會輸。

葉節點,win[v] = false, lose[v] = true.

其他節點 win[v] = win[v] | !win[child], lose[v] = lose[v] | !lose[child]. (因為每次贏的人,下一個就不是先手,所以結果肯定是跟下一個節點的贏成對立關系)。


如若win[0] = true , lose[0] = true則意味著第一局的人可以操控結果,否則按照k的次數來判斷是否可以贏。

代碼:

#include 
#include 
#define N_max 123456
#define sigma_size 26

using 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);
	else
		ans(0);
}

int main() {
	init();
	solve();
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved