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. eclipse到Android Studio的项目迁移

    一直以来.公司开发都是用eclipse.可是随着我们应用不断成长.项目结构越来越庞大.项目间依赖关系变得非常复杂.用eclipse管理显得非常吃力,常常一个同事更改依赖项目之后,别人在更新.都会出现故 ...

  2. C++求解数组中出现超1/4的三个数字。

    #include <iostream> using namespace std; //求x!中k因数的个数. int Grial(int x,int k) { int Ret = 0; w ...

  3. [DevExpress]GridControl分页的实现

    加入两个组件:BindingNavigator和BindingSource 代码: using System; using System.Collections.Generic; using Syst ...

  4. ci url 控制 查询 通过路由 控制返回 视图的结果

    http://192.168.2.102/fastdatav/ChkUrl/daily/G8 http://192.168.2.102/fastdatav/ChkUrl/daily/ 放在 试图  控 ...

  5. bzoj2709: [Violet 1]迷宫花园

    二分答案,spfa check就行了. gb题卡精度. #include<cstdio> #include<iostream> #include<cstring> ...

  6. 【Silverlight】Bing Maps学习系列(六):使用扩展模式(Extended Modes)(转)

    [Silverlight]Bing Maps学习系列(六):使用扩展模式(Extended Modes) 微软Bing Maps推出有有段时间了,通过不断的改进和新的地图更新,现在已经基本上形成了一套 ...

  7. [翻译]NUnit--前言(一)

    前言: 翻译这个系列主要是自己在学习NUnit之时看英文文档大部分能看懂但是有些还是功底不足,所以在方便自己以后再学习的时候可以快速查找,也能够加深印象以及掌握的更好.同时在搜索网上关于NUnit系列 ...

  8. BZOJ_3171_[Tjoi2013]循环格_最小费用最大流

    BZOJ_3171_[Tjoi2013]循环格_最小费用最大流 Description 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子.每个元素有一个坐标(行,列),其中左上角元素坐标为 ...

  9. oracle 定时器调用存储过程

    转载请说明出处:http://t22011787.iteye.com/blog/1112745 一.查询系统中的job,可以查询视图 --相关视图 select * from dba_jobs; se ...

  10. Objective-C 继承与类

    创建: 2018/01/20 完成: 2018/01/21 更新: 2018/01/22 标题前增加 [Objective-C]  继承的概念  父类与子类 ●继承: 继承其他类 ●父类: 被继承的类 ...