码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 455B A Lot of Games (Trie + dfs)

时间:2014-08-09 21:32:19      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   for   2014   ar   amp   new   

题目大意:

两个人往一个空的字符串里填单词,每一次只能填一个,而且填完之后要是给出的N个字符串的前缀。


思路分析:

先用给出的所有单词建字典树。

然后从根节点开始dfs。

win [x] 表示踩在x节点上是否有必胜策略

lose [x] 表示踩在x节点上是否有必败策略。

然后是博弈的过程。

如果先手有必胜和必败的策略,那么他可以一直输到k-1

如果只有必胜策略。那么只有当k为奇数的时候它才能赢。

其他为后手赢。


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define maxn 200005
using namespace std;

struct node
{
    node * next[26];
};
node trie[maxn],*root;
int win[maxn],lose[maxn],tot=0;

node *New()
{
    node *t = &trie[tot++];
    memset(t->next,NULL,sizeof(t->next));
    return t;
}
void Insert(char *str,int len)
{
    node *p=root;
    for(int i=0;i<len;i++)
    {
        int to=str[i]-'a';
        if(p->next[to]==NULL)p->next[to]=New();
        p=p->next[to];
    }
}

void dfs(node *r)
{
    int u=r-trie,cnt=0;
    win[u]=lose[u]=0;
    for(int i=0;i<26;i++)
    {
        if(r->next[i]!=NULL)
        {
            dfs(r->next[i]);
            cnt++;
            int v=r->next[i]-trie;
            if(!win[v])win[u]=1;
            if(!lose[v])lose[u]=1;
        }
    }
    if(!cnt)lose[u]=1;
}
char str[maxn];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    root=New();
    for(int i=0;i<n;i++)
    {
        scanf("%s",str);
        Insert(str,(int)strlen(str));
    }

    dfs(root);
    if(win[0] && lose[0])puts("First");
    else if(win[0] && (k&1))puts("First");
    else puts("Second");
    return 0;
}


codeforces 455B A Lot of Games (Trie + dfs),布布扣,bubuko.com

codeforces 455B A Lot of Games (Trie + dfs)

标签:blog   os   io   for   2014   ar   amp   new   

原文地址:http://blog.csdn.net/u010709592/article/details/38459761

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!