CF 455B-- A Lot of Games

本文介绍了一个由三人发明的字符串游戏,玩家需合作构建单词并确保其为给定字符串组的前缀。通过构建字典树并采用递推方法确定最优策略下的胜者。

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

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
思路:构造字典树,自底向上递推一下。fuck[x][0]为1表示踩在节点x上能让自己输。fuck[x][1]为1表示踩在节点x上能让自己赢
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;
#define inf 0x3f3f3f3f
#define maxn 100080
#define LL long long int
char str[maxn];
struct Node
{
	int id,dep;
	bool operator < (const Node & a)const
	{
		return dep < a.dep;
	}
}node[maxn];
struct Trie
{
	int first[maxn],ch[maxn],nxt[maxn],fuck[maxn][2];
	int cnt;
	void init()
	{
		memset(fuck,0,sizeof(fuck));
		cnt = 1;
		first[0] = -1;
	}
	int idx(char c)
	{
		return c - 'a';
	}
	void insert(char * s)
	{
		int u = 0,j;
		int len = strlen(s);
		for(int i = 0;i < len;i++)
		{
			int id = idx(s[i]);
			bool flag = false;
			for(j = first[u];j != -1;j = nxt[j])
			{
				if(ch[j] == id)
				{
					flag = true;
					break;
				}
			}
			if(flag)	u = j;
			else 
			{
				node[cnt].id = cnt;
				node[cnt].dep = i+1;
				ch[cnt] = id;
				first[cnt] = -1;
				nxt[cnt] = first[u];
				first[u] = cnt++;
				u = cnt-1;
			}
		}
	}
	void gao()
	{
		sort(node+1,node+cnt);
		for(int i = cnt-1;i >= 1;i--)//能让自己赢
		{
			bool flag = true;
			for(int j = first[node[i].id];j != -1;j = nxt[j])
			{
				if(fuck[j][1])
				{
					flag = false;
					break;
				}
			}
			fuck[node[i].id][1] = flag;
		}
		for(int i = cnt-1;i >= 1;i--)//踩了这一步能让自己输吗
		{
			bool flag = true;
			for(int j = first[node[i].id];j != -1;j = nxt[j])
			{
				if(fuck[j][0])
				{
					flag = false;
					break;
				}
			}
			fuck[node[i].id][0] = flag;
			if(first[node[i].id] == -1)	fuck[node[i].id][0] = 0;
		}
		for(int i = first[0];i != -1;i = nxt[i])
		{
			if(fuck[i][0])	fuck[0][0] = 1;
			if(fuck[i][1])	fuck[0][1] = 1;
		}
	}
}trie;
int main()
{
	//freopen("in.txt","r",stdin);
	int n,k;
	while(scanf("%d%d",&n,&k)==2)
	{
		trie.init();
		for(int i = 1;i <= n;i++)
		{
			scanf("%s",str);
			trie.insert(str);
		}
		trie.gao();
		if(trie.fuck[0][0] && trie.fuck[0][1])	cout << "First" << endl;
		else if(trie.fuck[0][0])
		{
			cout << "Second" << endl;
		}
		else if(trie.fuck[0][1])
		{
			if(k&1)	cout << "First" << endl;
			else cout << "Second" << endl;
		}
		else cout << "Second" << endl;
	}
}


uni-app提供了内置的微信分享API,可以通过调用相关方法来实现微信小程序的分享功能。首先,在onLoad方法中使用wx.showShareMenu方法,设置menus将发送给朋友和分享到朋友圈两个按钮都可以点击。具体代码如下: ```javascript onLoad() { wx.showShareMenu({ withShareTicket: true, menus: \["shareAppMessage", "shareTimeline"\] }) } ``` 接下来,需要分别编写发送给朋友和分享到朋友圈的方法。这两个方法应该与data和methods等同级,不要写到methods里面。具体代码如下: ```javascript onShareAppMessage(res) { if (res.from === 'button') { console.log(res.target) } return { title: 'title', path: '/pages/hfdt/gztjh', mpId: 'wx6bf107b87c455b99' } }, onShareTimeline(res) { return { title: '胶南街道召开“红帆支部”观摩学习暨工作推进会', type: 0, summary: "" } } ``` 在onShareAppMessage方法中,可以设置分享的标题、路径和微信小程序的AppId。在onShareTimeline方法中,可以设置分享到朋友圈的标题和类型等信息。通过以上步骤,就可以实现uni-app微信小程序的分享功能。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [uniapp 微信分享](https://blog.youkuaiyun.com/lxgyydsgod/article/details/126234984)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [uniapp微信小程序使用分享功能](https://blog.youkuaiyun.com/qq_43080548/article/details/125619531)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值