区间DP的博弈论模型: Letter Picking
Letter Picking
题面翻译
题目描述
Alice 和 Bob 在玩游戏。
给出一个长度为偶数的,非空的且仅含小写字母的字符串 。每个玩家还拥有一个初始为空的字符串。
Alice 先手,两名玩家交替行动。在一次行动中,玩家可以取 首或尾字符,将其从 中移除后加入到自己的字符串的 最前面。
当 为空时游戏结束,拥有字典序更小的字符串的玩家获胜。若两名玩家的字符串相等则平局。
若 Alice 和 Bob 都足够聪明,判断谁会取胜,或者游戏为平局。
数据组数 ,。保证所有输入的 长度都为偶数。
题目描述
Alice and Bob are playing a game. Initially, they are given a non-empty string , consisting of lowercase Latin letters. The length of the string is even. Each player also has a string of their own, initially empty.
Alice starts, then they alternate moves. In one move, a player takes either the first or the last letter of the string , removes it from and prepends (adds to the beginning) it to their own string.
The game ends when the string becomes empty. The winner is the player with a lexicographically smaller string. If the players’ strings are equal, then it’s a draw.
A string is lexicographically smaller than a string if there exists such position that for all and .
What is the result of the game if both players play optimally (e. g. both players try to win; if they can’t, then try to draw)?
输入格式
The first line contains a single integer ( ) — the number of testcases.
Each testcase consists of a single line — a non-empty string , consisting of lowercase Latin letters. The length of the string is even.
The total length of the strings over all testcases doesn’t exceed .
输出格式
For each testcase, print the result of the game if both players play optimally. If Alice wins, print “Alice”. If Bob wins, print “Bob”. If it’s a draw, print “Draw”.
样例 #1
样例输入 #1
1 | 2 |
样例输出 #1
1 | Alice |
提示
One of the possible games Alice and Bob can play in the first testcase:
- Alice picks the first letter in : “orces”, “f”, “”;
- Bob picks the last letter in : “orce”, “f”, “s”;
- Alice picks the last letter in : “orc”, “ef”, “s”;
- Bob picks the first letter in : “rc”, “ef”, “os”;
- Alice picks the last letter in : “r”, “cef”, “os”;
- Bob picks the remaining letter in : “”, “cef”, “ros”.
Alice wins because “cef” < “ros”. Neither of the players follows any strategy in this particular example game, so it doesn’t show that Alice wins if both play optimally.
定义状态:
对于区间i~j,博弈后的结果,记1为先手胜,0为先手平,-1为先手负
考虑先手:

以上为先手必获胜的结果,当先手出招时候,后手想使用反制手段,但是发现后手的每一种选择都是使结果导向先手方胜利,那么后手方没办法只能输;
接下来讨论先手出招,但后手通过反制手段,但因为选择当中没有使后手方获胜的情况,但存在若干个平手的情况,于是后手方退而求其次,使最终结果导向平局的情况;
当然这里讨论的是先手不输的情况,先手不输的情况包括先手赢和先手平两种,如果用if+先手赢+else if+先手不输 来把先手赢的情况再第二种情况之前拒之门外,那么第二种情况就是先手不输的情况
即为
.png)
附上代码:
1 |
|