Codeforces 455B
B. A Lot of Gamestime limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
InputThe 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.
OutputIf the player who moves first wins, print "First", otherwise print "Second" (without the quotes).
Sample test(s)input2 3
a
boutputFirstinput3 1
a
b
coutputFirstinput1 2
aboutputSecond
Trie树存储字符串,我们需要保存到达某个结点时是否必胜或者是否存在必败态。
win[u]表示在结点u是否必胜, lose[u]表示在结点u是否存在必败态。
Accepted Code:
/*************************************************************************
> File Name: D.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月13日 星期三 14时10分37秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ const int maxn = ;
int ch[maxn][]; struct Trie {
int sz;
Trie() {
memset(ch[], , sizeof(ch[]));
sz = ;
}
void insert(char *s);
void dfs(int u);
}; inline void Trie::insert(char *s) {
int u = , n = (int)strlen(s);
for (int i = ; i < n; i++) {
int id = s[i] - 'a';
if (!ch[u][id]) {
memset(ch[sz], , sizeof(ch[sz]));
ch[u][id] = sz++;
}
u = ch[u][id];
}
} bool win[maxn], lose[maxn];
inline void Trie::dfs(int u) {
bool flag = false;
for (int i = ; i < ; i++) if (ch[u][i]) {
flag = true;
int v = ch[u][i];
dfs(v);
if (!win[v]) win[u] = true;
if (!lose[v]) lose[u] = true;
}
if (!flag) lose[u] = true;
} char str[maxn];
int main(void) {
int n, k;
Trie A;
scanf("%d %d", &n, &k);
while (n--) scanf("%s", str), A.insert(str);
memset(win, false, sizeof(win));
memset(lose, false, sizeof(lose));
A.dfs();
if ((win[] && lose[]) || (win[] && k&)) puts("First");
else puts("Second"); return ;
}
Codeforces 455B的更多相关文章
- Codeforces 455B A Lot of Games(字典树+博弈)
题目连接: Codeforces 455B A Lot of Games 题目大意:给定n.表示字符串集合. 给定k,表示进行了k次游戏,然后是n个字符串.每局開始.字符串为空串,然后两人轮流在末尾追 ...
- Codeforces 455B A Lot of Games
http://codeforces.com/contest/455/problem/B 题目大意: 给出n个字符串,进行k次游戏,每次游戏输家下次作为先手,游戏规则为每次放一个字母,导致当前构造的字符 ...
- Codeforces 455B A Lot of Games:博弈dp【多局游戏】
题目链接:http://codeforces.com/problemset/problem/455/B 题意: 给你n个字符串,然后进行k局游戏. 每局游戏开始有一个空串,然后双方轮流给这个串的末尾添 ...
- codeforces 455B A Lot of Games(博弈,字典树)
题目 参考自博客:http://blog.csdn.net/keshuai19940722/article/details/38455269 //字典树,博弈 根据当前节点的后续来确定当前节点的状态, ...
- CodeForces 455B A Lot of Games (博弈论)
A Lot of Games 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/J Description Andrew, Fedo ...
- Codeforces 455B A Lot of Games 字典树上博弈
题目链接:点击打开链接 题意: 给定n个字符串,k局游戏 对于每局游戏,2个玩家轮流给一个空串加入一个小写字母使得加完后的字符串不是n个字符串的前缀. 输家下一轮先手 问是先手必胜还是后手必胜 思路: ...
- ACM 博弈(难)题练习 (第一弹)
第二弹: 套路&&经验总结: 1. N堆***的游戏,一般可以打表找SG函数的规律.比如CodeForces 603C 2.看起来是单轮的游戏,实际上可能拆分成一些独立的子游戏.比如C ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
随机推荐
- JavaScript特效源码(1、文字特效)
注:本文以及以下关于Javascript特效源码都是分享自JavaScript源码大全. 1.逐隐逐现的的特效 逐隐逐现的文字特效[推荐使用][适用于IE4++] (修改显示的文字后根据说明进行共2步 ...
- 辨析JspWriter和PrintWriter
JspWriter和PrintWriter的区别? JspWriter相当于带缓冲的PrintWriter 如何控制out缓冲? 通过设置JSP页面page指令的buffer属性, 可以调整out缓冲 ...
- zabbix--------配置邮件报警功能---服务器上配置---------
--------配置邮件报警功能---服务器上配置--------- [www.aa.com@ ~]# yum install mailx -y [www.aa.com@ ~]# vi /etc/ma ...
- Genymotion 的那些事
一.什么是Genymotion? Genymotion是一套完整的工具,它提供了Android虚拟环境. 支持Windows.Linux和Mac OS等操作系统. 在Android开发中可以代替安卓模 ...
- PKU--3628 Bookshelf 2(01背包)
题目http://poj.org/problem?id=3628 分析:给定一堆牛的高度,把牛叠加起来的高度超过牛棚的高度. 且是牛叠加的高度与牛棚高度之差最小. 把牛叠加的高度看作是背包的容量,利用 ...
- @RestControllerAdvice作用及原理
原文:Spring Boot 系列(八)@ControllerAdvice 拦截异常并统一处理 在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@Exceptio ...
- css3之背景background-origin,background-clip,background-size
background-origin属性指定了背景图像的位置区域. content-box, padding-box,和 border-box区域内可以放置背景图像. background-clip用来 ...
- js 实现音频播放与暂停
html: <script src="js/jquery-2.1.3.min.js"></script> <div id="soundIco ...
- kafka数据祸福和failover
k CAP帽子理论. consistency:一致性 Availability:可用性 partition tolerance:分区容忍型 CA :mysql oracle(抛弃了网络分区) CP:h ...
- 2019-8-26-当-ASP.NET-Core-链接找不到时可能的原因
title author date CreateTime categories 当 ASP.NET Core 链接找不到时可能的原因 lindexi 2019-08-26 18:52:28 +0800 ...