zstu 4212 ——String Game ——————【字符串处理】
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 ——————【字符串处理】的更多相关文章
- .NET面试题解析(03)-string与字符串操作
系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 字符串可以说是C#开发中最常用的类型了,也是对系统性能影响很关键的类型,熟练掌握字符串的操作非常重要. 常 ...
- [CareerCup] 1.2 Reverse String 翻转字符串
1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...
- char型字符串(数组)与string型字符串 指针与引用
一.常指针: int *const p; //指针不可改变,但是指针指向的数据可以改变. 指向常量的指针: const int *p; //指针可以改变,但是指针指向的数据不可以改变. 指 ...
- Java基础知识强化59:String(字符串)和其他类型的相互转化
1. String类型 ---> 其他类型 (1)使用基本类型包装类的parseXXX方法 e.g:String(字符串)转化为int(整型) String MyNumber ="12 ...
- String[255]在高版本Delphi里还是被解释成Byte,总体长度256,使用StrPCopy可以给Array String拷贝字符串(内含许多实验测试)
学了好多不了解的知识: procedure TForm1.Button1Click(Sender: TObject); var s1 : String; s2 : String[]; begin s1 ...
- JavaScript String(字符串对象)
String 对字符串的支持 String.charAt( n ) 返回字符串中的第n个字符 n 是下标 String.charCodeAt( ) 返回字符串中的第n个字符的代码 String.con ...
- String:字符串常量池
String:字符串常量池 作为最基础的引用数据类型,Java 设计者为 String 提供了字符串常量池以提高其性能,那么字符串常量池的具体原理是什么,我们带着以下三个问题,去理解字符串常量池: 字 ...
- C# 数据类型之 String(字符串)
Ø 简介 在开发中最常见的数据类型就是 String 类型,即字符串类型.为什么要单独讨论下这个类型,是因为:它是系统内置的基础数据类型:它的使用频率非常高:它是一个特殊的引用类型.其实大家都会使用 ...
- Redis支持的数据类型及相应操作命令:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sorted set:有序集合)
help 命令,3种形式: help 命令 形式 help @<group> 比如:help @generic.help @string.help @hash.help @list.hel ...
随机推荐
- CH收藏的书
论语 道德经 墨子
- Timer(定时器)
默认情况下,在每个采样器之前没有任何延时,这样不能很好的模拟现实生活中人们访问网页,因为现实生活中人们点击一个请求后,会有一定的时间,然后再点击下一个请求,JMeter提供了定时器来模拟这种行为. 定 ...
- Multimap的初使用
之前不知道这个类型使用,在朋友的推荐下进行了个实际操作. 类似这种查询,我需要将他们归类拿出来,如果招以前那么拿的话可能要套挺多个循环的. 首先根据查询得到一个 List<Map<Stri ...
- C# Winform 加载窗体/对象时的等待页面设计
在设计应用程序过程中,有时候加载对象需时较长,我们可以显示一个Loading等待页面,对用户来说就比较友好了. 这个还是涉及到多线程,下面是步骤. 一.创建好Loading窗体: 一个Panel用于显 ...
- 【BZOJ1880】[SDOI2009]Elaxia的路线 (最短路+拓扑排序)
[SDOI2009]Elaxia的路线 题目描述 最近,\(Elaxia\)和\(w**\)的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间. \(El ...
- python学习之路---day05
字典一:基本组成 dic={"",[],{},"",2,} 字典由key 和value组成, key(键):键是不可变的(且必须要不可改变),一个字典中的key ...
- openmpi-3.0.1超线程报错问题
先简单记录一下,虽然还有一些疑惑没有解决. 之前安装openmpi是用的命令安装,版本比较低,mfix并行总出现死锁问题,于是想看看是不是openmpi版本导致,虽然目前还未找到具体原因,但是先记录下 ...
- Linux系统下Qt环境搭建
http://www.linuxidc.com/Linux/2013-07/87576.htm 主要是需要提前安装一些动态库,否则编译的时候会出现 /usr/bin/ld: cannot find - ...
- POJ_1984 Navigation Nightmare 【并查集】
一.题面 POJ1984 二.分析 这题还是比较有意思的一题. 首先需要清楚的是,这题与普通并查集的区别在于它的节点之间的权值是二维的,因为是曼哈顿距离,肯定不能直接存距离,这样将不利于后面的路径压缩 ...
- POJ_1733 Parity game 【并查集+离散化】
一.题面 POJ1733 二.分析 该题与之前做过的带权并查集的唯一区别就是数组开不下.所以需要用离散化的思想,只取那些有用的点来解决该问题. 离散化其实就是把这些所有用到的点收集后,去重,再排一下序 ...