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. Echarts+WPF

    C# using System; using System.Collections.Generic; using System.IO; using System.Linq; using System. ...

  2. Spring AOP的实现机制

    AOP(Aspect Orient Programming),一般称为面向切面编程,作为面向对象的一种补充,用于处理系统中分布于各个模块的横切关注点,比如事务管理,日志,缓存等等.AOP 实现的关键在 ...

  3. CF165D Beard Graph

    $ \color{#0066ff}{ 题目描述 }$ 给定一棵树,有m次操作. 1 x 把第x条边染成黑色 2 x 把第x条边染成白色 3 x y 查询x~y之间的黑边数,存在白边输出-1 \(\co ...

  4. eclipse创建Java项目时提示Open Associated Perspective?

    在eclipse中,原先使用python进行编程,需要新建java项目时,会提示如下信息: 消息框内翻译如下: Open Associated Perspective? --开放关联视角? This ...

  5. 在执行 Database.SqlQuery Method (String, Object[]) 执行中出现错误

      执行类似于   var  params =  new SqlParameter[]{--};   Database.SqlQuery<Type>(sql1,params);   Dat ...

  6. ArcGIS10.4破解

    arcgis 10.4破解方法 *注意!Desktop,Engine,Server必须为同一版本* Engine10.4破解失败,SDE10.4未进行破解 1.先安装License10.4 2.再安装 ...

  7. 查看 tensorflow 是GPU版本 还是CPU版本

    在Python环境中输入: import os from tensorflow.python.client import device_lib os.environ["TF_CPP_MIN_ ...

  8. Svn安装成功后的操作

    一.服务器端创建目录 在Svn服务器端创建一个用来保存客户端提交文件的文件夹 (我的路径是d:/Svn/App/book) 首先将Svn的服务端安装目录/bin 进行环境配置,以下操作是在进行环境配置 ...

  9. 【算法笔记】B1020 月饼

    1020 月饼 (25 分) 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意 ...

  10. BZOJ - 1458 / P4311 最大流应用 贪心

    题意:给定n*m的图,每个士兵可以占领当前行和列,第i行至少要R[i]个士兵占领,第j列至少要C[j]个士兵占领,部分网格无法占领,求占领所用最少士兵数,若无解则输出orz 士兵的贡献情况有1(只有效 ...