4212: String Game

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 337  Solved: 41

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 consists of 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

题目大意:给你两个串a,b。从Bob开始,Bob和Alice两人轮流从a串左端或者右端删去一个字符,当跟b串长度相同时停止。如果a串这时是等于b串的,则Alice赢,否则Bob赢。问你最后谁赢。

解题思路:大家都能想到,如果a串中含有b串,且b串真好是位于a串的中间时,a串此时可以看做三部分,左部分,b串,右部分。这时肯定是Alice赢。Bob取一端,Alice取另一端。如:acfdebb fde。另外就是a串中含有两个b串,看作是平移2个位置。如:xyxyx xyx。这种也是Alice赢,因为Bob取一端,Alice跟他取同一端。还有就是出现平移一个位置的时候。如: acce c、 abccccba ccc。总体上是当两个串的奇偶性相同时作为一种情况,不同时作为另一种情况来处理。

(⊙o⊙) 代码写得乱得恶心。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
string A,B;
bool jud(int n,int m){
int pos = (n-m)/2;
for(int i = 0; i < m; i++){
if(A[i+pos] != B[i]){
return 0;
}
}
for(int i = 0; i < m; i++){
if(A[i+pos+1] != B[i]){
return 0;
}
}
return true;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
cin>>A>>B;
int n = A.size(), m = B.size();
if(n % 2 != m % 2){
printf("%s\n",jud(n,m) ? "Alice" : "Bob");
continue;
}
int mmid = m/2, nmid = n/2;
int flag1 = 1;
int i, j;
for( i = nmid, j = mmid; i >= 0 && j >= 0; i--,j--){
if(A[i] != B[j]){
flag1 = 0;
}
}
int cs = i+1;
for( i = nmid+1, j = mmid+1; i < n && j < m; j++,i++){
if(A[i] != B[j]){
flag1 = 0;
}
}
int ce = n - i;
int flag2 = 1;
for( i = nmid - 1, j = mmid; i >= 0 && j >= 0; j--,i--){
if(A[i] != B[j]){
flag2 = 0;
}
}
int ccs = i+1;
for( i = nmid, j = mmid+1; i < n && j < m; j++,i++){
if(A[i] != B[j]){
flag2 = 0;
}
}
for(i = nmid + 1, j = mmid; i >= 0 && j >= 0; j--,i--){
if(A[i] != B[j]){
flag2 = 0;
}
}
for(i = nmid + 2, j = mmid+1; i < n && j < m; j++,i++){
if(A[i] != B[j]){
flag2 = 0;
}
}
int cce = n - i;
if(cs != ce){
flag1 = 0;
}
if(ccs != cce){
flag2 = 0;
}
if(flag1 || flag2){
puts("Alice");
}else{
puts("Bob");
}
}
return 0;
} /* 55
acce c
abccccde ccc
xxx xx
xxc xx
cxxd xx
xyxyxyx xyxyx
aba b
bab b
aaab aab
xyz mnk
xyz xyz
*/

  

zstu 4212 ——String Game ——————【字符串处理】的更多相关文章

  1. .NET面试题解析(03)-string与字符串操作

      系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 字符串可以说是C#开发中最常用的类型了,也是对系统性能影响很关键的类型,熟练掌握字符串的操作非常重要. 常 ...

  2. [CareerCup] 1.2 Reverse String 翻转字符串

    1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...

  3. char型字符串(数组)与string型字符串 指针与引用

    一.常指针: int *const p;    //指针不可改变,但是指针指向的数据可以改变. 指向常量的指针: const int *p;    //指针可以改变,但是指针指向的数据不可以改变. 指 ...

  4. Java基础知识强化59:String(字符串)和其他类型的相互转化

    1. String类型 ---> 其他类型 (1)使用基本类型包装类的parseXXX方法 e.g:String(字符串)转化为int(整型) String MyNumber ="12 ...

  5. String[255]在高版本Delphi里还是被解释成Byte,总体长度256,使用StrPCopy可以给Array String拷贝字符串(内含许多实验测试)

    学了好多不了解的知识: procedure TForm1.Button1Click(Sender: TObject); var s1 : String; s2 : String[]; begin s1 ...

  6. JavaScript String(字符串对象)

    String 对字符串的支持 String.charAt( n ) 返回字符串中的第n个字符 n 是下标 String.charCodeAt( ) 返回字符串中的第n个字符的代码 String.con ...

  7. String:字符串常量池

    String:字符串常量池 作为最基础的引用数据类型,Java 设计者为 String 提供了字符串常量池以提高其性能,那么字符串常量池的具体原理是什么,我们带着以下三个问题,去理解字符串常量池: 字 ...

  8. C# 数据类型之 String(字符串)

    Ø  简介 在开发中最常见的数据类型就是 String 类型,即字符串类型.为什么要单独讨论下这个类型,是因为:它是系统内置的基础数据类型:它的使用频率非常高:它是一个特殊的引用类型.其实大家都会使用 ...

  9. Redis支持的数据类型及相应操作命令:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sorted set:有序集合)

    help 命令,3种形式: help 命令 形式 help @<group> 比如:help @generic.help @string.help @hash.help @list.hel ...

随机推荐

  1. C#中如何向数组中动态添加元素

    转自:https://blog.csdn.net/qq_35938548/article/details/78325558 背景:现需要向数组中循环插入字符串,但C#中的数组是不支持动态添加元素的,只 ...

  2. .NET中的程序集

    参考:http://blog.sina.com.cn/s/blog_7ade159d0102wmg9.html 程序集(Assembly,装配件,.NET程序集) 程序集是应用程序的部署单元,.NET ...

  3. 模拟Springboot二:内置tomcat

    既然要将tomcat内置到项目中,并且能够成功的启动项目就要知道 tomcat  做了哪些事情 ,那么就必须先搞明白 一个 普通的web项目是如何被我们本地配置的tomcat启动并运行的 (1). 先 ...

  4. 标准库函数begin和end------c++primer

    尽管能计算得到尾后指针,但这种用法极易出错.为了让指针的使用更简单.更安全,c++新标准引入了两个名为begin和end的函数.这两个函数与容器中的两个同名成员功能类似,不过数组毕竟不是类类型,因此这 ...

  5. stream.fail() eof() EOF

    ifstream infile("student.dat"); while((c=infile.get())!=EOF){}    //EOF的值是-1 但在 #include&l ...

  6. Agreement has been updated--Edit Phone Number最便捷解决办法(不需要安全提示问题和双重认证)

    2018年06月04日亲测有效: CSDN博客跳转网址:

  7. loj #6122. 「网络流 24 题」航空路线问题

    #6122. 「网络流 24 题」航空路线问题 题目描述 给定一张航空图,图中顶点代表城市,边代表两个城市间的直通航线.现要求找出一条满足下述限制条件的且途经城市最多的旅行路线. 从最西端城市出发,单 ...

  8. 【SSO单点系列】(7):CAS4.0 SERVER通过数据库方式认证用户

    在前几篇中有简单介绍服务端的认证方式,默认的是直接在 deployerConfigContext.xml 文件中 一个叫做 primaryAuthenticationHandler 的bean中配置. ...

  9. Resurrectio-capserjs的自动化脚本录制工具

    [根据github上的文档说明整理] Phantom下的任何操作都可以录制 Resurrectio是一个Chrome插件,他可以记录浏览器的操作,并转化成对应的casperjs脚本 Resurrect ...

  10. 搭建 docker + nginx + keepalived 实现Web应用的高可用(亲测)

    1. 环境准备     下载 VMware : https://www.vmware.com/go/getplayer-win        下载 Centos : https://mirrors.a ...