A Lot of Games - CodeForces #260 (Div. 2) D Trie树

本文介绍了一个涉及字符串构建的游戏,两名玩家轮流操作以形成一个符合预设字符串组前缀的单词。通过对Trie树的构建与分析,确定了初始行动者在不同轮次中的最优策略,确保最终胜利。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Sample test(s)
input
2 3
a
b
output
First
input
3 1
a
b
c
output
First
input
1 2
ab
output
Second

思路:通过Trie树,得到以下几种情况,先手是否可以必胜和先手是否可以必败,然后先手会根据k的大小使自己尽可能去赢。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
char s[100010];
struct Trie
{ int index1,index2;
  Trie *next[26];
  Trie()
  { index1=-1;index2=-1;
    memset(next,0,sizeof(next));
  }
};
Trie *root=new Trie;
void Trie_Insert(Trie *tr,char *s)
{ if(*s!='\0')
  { if(tr->next[*s-'a']==0)
     tr->next[*s-'a']=new Trie;
    Trie_Insert(tr->next[*s-'a'],s+1);
  }
}
void dfs(Trie *tr)
{ int i,j,k=0;
  tr->index1=0;
  tr->index2=1;
  for(i=0;i<=25;i++)
   if(tr->next[i]!=0)
   { k=1;
     dfs(tr->next[i]);
     Trie *son=tr->next[i];
     if(son->index1==0)
      tr->index1=1;
     if(son->index2==1)
      tr->index2=0;
   }
  if(k==0)
   tr->index2=0;
}
int main()
{ int n,m,i,j,k;
  bool flag1=false,flag2=false;
  scanf("%d%d",&n,&m);
  for(i=1;i<=n;i++)
  { scanf("%s",s);
    Trie_Insert(root,s);
  }
  dfs(root);
  if(root->index1==1)
   flag1=true;
  if(root->index2==0)
   flag2=true;
  if(flag1 && flag2)
   printf("First\n");
  else if(flag1)
  { if(m&1)
     printf("First\n");
    else
     printf("Second\n");
  }
  else
   printf("Second\n");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值