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. Django打造大型企业官网(二)

    三.项目环境搭建 3.1.创建项目环境和安装包 创建django项目 mkvirtualenv DjangoProject workon DjangoProject pip install -i ht ...

  2. iOS 保存视频AVAssetWriter

    错误的CMTime导致保存的视频无效,比如: frameTime CMTime 1122 600ths of a second value CMTimeValue 1122timescale CMTi ...

  3. 下载、编译、运行android 7.1系统(ubuntu 16.0.4)【转】

    本文转载自:http://blog.csdn.net/andywuchuanlong/article/details/53977710

  4. YTU 2543: 数字整除

    2543: 数字整除 时间限制: 1 Sec  内存限制: 128 MB 提交: 33  解决: 8 题目描述 定理:把一个至少两位的正整数的个位数字去掉,再从余下的数中减去个位数的5倍.当且仅当差是 ...

  5. UVa10288 Coupons 分数模版

    UVa10288 题目非常简单, 答案就是 n/n+n/(n-1)+...+n/1; 要求分数输出.套用分数模板.. #include <cstdio> #include <cstr ...

  6. E20170612-sl

     tampon n.     卫生棉塞; 止血棉塞;    sanitary  n. 公共厕所;  adj.     卫生的; 清洁的;    belonging n.     附属品,附件,属性; ...

  7. 大数高精度加减乘除 51nod 1005 大数加法

    1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B ...

  8. 题解报告:hdu 1035 Robot Motion(简单搜索一遍)

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

  9. css为tbody或者li奇数偶数行样式

    <style> table tbody tr:nth-child(odd){ background:#fff; } table tbody tr:nth-child(even){ back ...

  10. mysqli 进一步分析

    1. 一.mysql与mysqli的概念相关: 1.mysql与mysqli都是php方面的函数集,与mysql数据库关联不大. 2.在php5版本之前,一般是用php的mysql函数去驱动mysql ...