codeforces_455B
1 second
256 megabytes
standard input
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.
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.
If the player who moves first wins, print "First", otherwise print "Second" (without the quotes).
2 3
a
b
First
3 1
a
b
c
First
1 2
ab
Second 题意:有n(n<=100000)个单词,两个人比赛,规则为:最开始字符串为空,两人轮流每次填一个字母,使该字符串为这n个单词其中一个的前缀,当一个人不能再填入字母时,他就输了,比赛进行k次,第k次比赛胜者为最终胜者。
思路:最开始毫无思路,跟着别人的题解走了一遍。将所有单词构建一颗字典树,再进行博弈。第一道博弈和字典树。1表示先手败,2表示先手胜,3表示能胜能负。先手必败则,先手一直先手,最后一局后手胜;先手胜则下一局成后手,即为胜败交替,此时,最后一句的胜败决定于k的奇偶性;能胜能败,则先手前k-1局败,最后一句先手且胜。简单博弈。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<malloc.h>
using namespace std;
#define maxn 100005 char s[maxn];
//int next[maxn][30],root,top; struct Trie
{
Trie *next[];
}; Trie* createtrie(char *str,Trie* root) //创建字典树
{
int len=strlen(str);
Trie *p=root,*q;
for(int i=; i<len; i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
{
q=(Trie *)malloc(sizeof(Trie));
//q->v=1;
for(int j=; j<; j++)
q->next[j]=NULL;
p->next[id]=q;
p=p->next[id];
}
else
{
//p->next[id]->v++;
p=p->next[id];
}
}
//p->v=-1;
return root;
} int solve(Trie *root) //博弈
{
int ok=,ans=;
for(int i=; i<; i++)
{
if(root->next[i]!=NULL)
{
ok=;
ans|=solve(root->next[i])^;
}
}
if(!ok)
ans=;
return ans;
} int main()
{
int n,k;
//top=root=1;
Trie* root;
scanf("%d%d",&n,&k);
root=(Trie*)malloc(sizeof(Trie));
for(int j=; j<; j++)
root->next[j]=NULL;
for(int i=; i<n; i++)
{
scanf("%s",s); root=createtrie(s,root);
}
//output(root);
int tmp=solve(root);
//cout<<tmp<<endl;
if(tmp==)
printf("First\n");
else if(k&&&tmp==)
printf("First\n");
else
printf("Second\n" );
return ;
}
codeforces_455B的更多相关文章
随机推荐
- 浅谈MySQL Capabilities --从调研PHP mysqlnd源码细节角度认识
今天一起来研究下MySQL Capabilities,这个非常重要,如果大家有想法自己动手实现一个MySQL客户端或者Proxy工具,那么就得先了解一下这块,正好PHP 5.3以上版本由于官方为了规避 ...
- SDUST 2844-Mineral Water(数学)
Mineral Water nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit1000ms Memory Limi ...
- [LeetCode][Java] Jump Game II
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- UVA 11488 Hyper Prefix Sets (Trie)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 蓝桥 PREV-34 历届试题 矩阵翻硬币
历届试题 矩阵翻硬币 时间限制:1.0s 内存限制:256.0MB 问题描述 小明先把硬币摆成了一个 n 行 m 列的矩阵. 随后,小明对每一个硬币分别进行一次 Q 操作. 对第 ...
- ios34---GDC,dispatch_once
// // ViewController.m // 09-掌握-GCD常用函数 // // Created by xiaomage on 16/2/18. // Copyright © 2016年 小 ...
- ARM+linux系统移植3G拨号上网收发短信(三)【转】
本文转载自:http://blog.csdn.net/hanmengaidudu/article/details/17099755 一.用text查看模式下面的“发”是指我敲的命令,“收”是指回车后显 ...
- Kentico中的skin.css的加载
kentico7中有如下的css引用 第一行的css是 SELECT * FROM dbo.CMS_CssStylesheet表中的css 后面2个对应到的是Kentico7\App_Themes\ ...
- 使用spring的DefaultResourceLoader自定义properties文件加载工具类
转自:https://www.cnblogs.com/zrbfree/p/6230957.html import java.io.IOException; import java.io.InputSt ...
- 树网的核 2007年NOIP全国联赛提高组(floyed)
树网的核 2007年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description [问题描述]设 T= ...