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. 修改Tomcat主目录

    在默认安装后,tomcat的主目录是webapps/root目录,如果我们想改变tomcat的主目录的话可以这样做: 1 %TOMCAT_HOME%/webapps/下直接创建,这种方法有一个缺点,就 ...

  2. 大数据处理框架之Strom:事务

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 storm-0.9 apache-flume-1.6.0 ...

  3. golang文件处理函数openfile与linux系统的文件函数的耦合

    golang运行最理想的环境是linux系统中,编译速度和执行速度都比较快,本文是关于golang中的文件操作函数 在golang标准库中os包提供了不依赖平台的借口,但是使用的风格是unix风格的. ...

  4. leecode刷题(3)-- 旋转数组

    leecode刷题(3)-- 旋转数组 旋转数组 给定一个数组,将数组中的元素向右移动 K 个位置,其中 K 是非负数. 示例: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5, ...

  5. Python之路Python作用域、匿名函数、函数式编程、map函数、filter函数、reduce函数

    Python之路Python作用域.匿名函数.函数式编程.map函数.filter函数.reduce函数 一.作用域 return 可以返回任意值例子 def test1(): print(" ...

  6. loj #535. 「LibreOJ Round #6」花火 树状数组求逆序对+主席树二维数点+整体二分

    $ \color{#0066ff}{ 题目描述 }$ 「Hanabi, hanabi--」 一听说祭典上没有烟火,Karen 一脸沮丧. 「有的哦-- 虽然比不上大型烟花就是了.」 还好 Shinob ...

  7. docker构建mysql容器及Navicat 远程连接

    1. MySQL部署 1.1拉取MySQL镜像 docker pull mysql 查看镜像 docker images 1.2创建MySQL容器 首先建立所需要的 文件夹: docker run - ...

  8. C#直接使用DllImport外部Dll的方法

    C#.Net调用基本格式:[DLLImport(“DLL文件路径”)]修饰符 extern 返回值类型 方法名称(参数列表) 如: [DllImport("kernel32.dll" ...

  9. 谈谈easyui datagrid 的数据加载

    文章目录 1url方式加载数据 1.1调用方式 1.2相关方法 1.3二次加载问题 2加载本地数据方式 2.1调用方式 2.2如何分页 2.3加载中效果 2.4如何不统计总数 这篇文章只谈jQuery ...

  10. ELK 实用架构