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 ≤ 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).

Examples
input
2 3
a
b
output
First
input
3 1
a
b
c
output
First
input
1 2
ab
output
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的更多相关文章

随机推荐

  1. .NET中的PublicKeyToken以及强命名问题

    在.NET的GAC出现之前,以前有DLL Hell的问题.这是由于当时对于共享的DLL的处理方式.是通过採用注冊表的方式实现的.当我们安装一个程序A的时候,这个程序包括一个共享的DLL,那么这个DLL ...

  2. 2016/2/18 html 标签 表格

    ①打开dreamw新建html 开头显示如下 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  3. (七)Java 变量类型

    Java 变量类型 在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifier [ = value][, identifier [= value] ...] ...

  4. 使用逆向工程生成mybatis的Mapper文件

    之前有写过一篇博客: 使用MyBatis Generator自动生成MyBatis的代码链接:http://www.cnblogs.com/klslb/p/6908535.html 这个太麻烦了,而且 ...

  5. 【bzoj1260】[CQOI2007]涂色paint

    题意:就是说一开始一个序列是空的,然后每次可以将连续的一段染成同一颜色,问多少次才能到目标状态. 一开始想的是二分,然后题解DP... f[i][j]表示区间[i,j]需要染色多少次 首先初始状态是f ...

  6. chmod a+w . 权限控制 su、sudo 修改文件所有者和文件所在组

    对当前目录对所有用户开放读写权限 chmod a+r . $ sudo chmod -R a+w /usr/lib/python2.7 所有用户添加文件的写权限 [linux]su.sudo.sudo ...

  7. luence优化速度

    一. .索引优化背景 很多网站都有自己的搜索引擎,比如百度,搜狗等等,而他们每天添加的索引量可想而知多么庞大,所以为了能提升用户的搜索响应速度,好的优化方案必不可少:当然对于一些网站的站内搜索也很有必 ...

  8. [NOI2018]冒泡排序

    https://www.zybuluo.com/ysner/note/1261482 题面 戳我 \(8pts\ n\leq9\) \(44pts\ n\leq18\) \(ex12pts\ q_i= ...

  9. linux下的zookeeper启动

    zookeeper的安装目录:/usr/local/zookeeper-3.4.6/bin/zkServer.sh; 配置文件路径:../conf/zoo.cfg 端口 :2181: ZooKeepe ...

  10. jQuery:has()和jQuery:contains()及jQuery:empty

    jQuery:has()和jQuery:contains()两个方法比较类似.不同点在于: has是判断标签的 contains是判断文本的 1.jQuery:has() <div>< ...