ZSTUOJ 4212 String Game
2015-12-22 11:40
253 查看
4212: String Game
Time Limit: 1 Sec Memory Limit:128 MBSubmit: 317 Solved:32
Description
Alice and Bob are playing the following game with strings of letters.Before the game begins, an initial string and a target string are decided. The initial string is at least as long as the target string. Then, Alice and Bob take turns, starting with the initial string. Bob goes first. In each turn, the current player
removes either the first or the last letter of the current string. Once the length of the current string becomes equal to the length of the target string, the game stops. If the string at the end of the game is equal to the target string, Alice wins the game;
otherwise Bob wins.
Determine who will win the game if both players are playing optimally.
Input
Each test case starts with N, the number of inputs to process. Each input consists of one line, which contains the initial string, followed by a space, followed by the target string. Each string consistsof only lowercase letters. The total input length will be less than 500000 characters.
Output
For each input, output the winner, which will either be Alice or Bob.Sample Input
5 aba b bab b aaab aab xyz mnk xyz xyz
Sample Output
Alice Alice Bob Bob Alice 题意:给你两个串s,a,Bob和Alice可以将a串任意从前段或后段拿走一个字符,当a串的长度与b串相等时,如果a串等于b串,则Alice赢,否则Bob赢。 方法:判断a串是否在b串的中间,或者是存在另外一种情况abababa ababa 若符合,则Alice,否Bob 如果a串中所有的元素与b串中相等,也是符合的 eg: aaa a
#include <bits/stdc++.h> using namespace std; const int maxn = 500010; char s[maxn],a[maxn]; bool Judge(int len1, int len2) { if(len1 >= len2) { int pos = (len1 - len2) >> 1; bool flag = true; if(len1 % 2 == len2 % 2) { for(int i=0; i<len2; i++) { if(a[i] != s[i+pos]) { flag = false; break; } } if(flag) return true; if(len1 - len2 > 1) { flag = true; for(int i=0; i<len2; i++) { if(a[i] != s[i+pos+1]) { flag = false; break; } } for(int i=0; i<len2; i++) { if(a[i] != s[i+pos-1]) { flag = false; break; } } if(flag) return true; } } else { for(int i=0; i<len2; i++) { if(a[i] != s[i+pos]) { flag = false; break; } } for(int i=0; i<len2; i++) { if(a[i] != s[i+pos+1]) { flag = false; break; } } if(flag) return true; } } return false; } int main() { int T; cin>>T; while(T-- && cin>>s>>a) { int len1 = strlen(s); int len2 = strlen(a); if(Judge(len1,len2)) cout<<"Alice"<<endl; else cout<<"Bob"<<endl; } return 0; }
相关文章推荐
- ssh2的整合
- Oracle11g数据库导入Oracle10g问题
- Think In Java 读后感
- HTML5 预览预加载文件,图片
- Hive的那些事儿?
- Xcode7.1以上版本的第三方库的导入解决问号问题
- Mac上eclipse安装SVN+JavaHL
- Linux聊天室项目 -- ChatRome(select实现)
- fusioncharts一个同事使用的饼图包
- 最近遇到的一些错误,以解决,进行记录
- HDU ACM 2586 How far away ?LCA->并查集+Tarjan(离线)算法
- 手机模拟位置实现精准位置营销效果分析
- mysql自定义循环函数
- iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总
- 分享Android微信红包插件
- mysql数据表中文乱码解决方案
- 报表练习笔记三——我的代码
- java中各种路径获取问题
- 安全演练组织流程图
- Java算法---华为oj迷宫问题求解(广度优先搜索)