AABB

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

TC

Description

One day, Jamie noticed that many English words only use the letters A and B. Examples of such words include "AB" (short for abdominal), "BAA" (the noise a sheep makes), "AA" (a type of lava), and "ABBA" (a Swedish pop sensation).

Inspired by this observation, Jamie created a simple game. You are given two strings: initial and target. The goal of the game is to find a sequence of valid moves that will change initial into target. There are two types of valid moves:
Add the letter A to the end of the string.
Reverse the string and then add the letter B to the end of the string.
Return "Possible" (quotes for clarity) if there is a sequence of valid moves that will change initial into target. Otherwise, return "Impossible".

Input

-
The length of initial will be between 1 and 999, inclusive.
-
The length of target will be between 2 and 1000, inclusive.
-
target will be longer than initial.
-
Each character in initial and each character in target will be either 'A' or 'B'.

Output

Class:
ABBA
Method:
canObtain
Parameters:
string, string
Returns:
string
Method signature:
string canObtain(string initial, string target)
(be sure your method is public)

Sample Input

"BBBBABABBBBBBA"

"BBBBABABBABBBBBBABABBBBBBBBABAABBBAA"

Sample Output

"Possible"

HINT

题意

给你一个字符串和一个目标串,通过下列两种操作,问你能不能从字符串跑到目标串:

在后面加一个A

翻转后,再后面加一个B

题解:

倒推,倒着推的顺序是确定的

至于交换和删除,都用标记就好了,是O(1)的

代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std; class ABBA{
public:
int a[];
int b[];
string canObtain(string initial, string target){
for(int i=;i<initial.size();i++)
if(initial[i]=='A')
a[i]=;
else
a[i]=;
for(int i=;i<target.size();i++)
if(target[i]=='A')
b[i]=;
else
b[i]=; int l=,r=target.size()-;
while(abs(l-r)+!=initial.size())
{ if(b[r]==)
{
if(l<r)
r--;
else
r++;
swap(l,r);
}
else
{
if(l<r)
r--;
else
r++;
}
}
int flag=;
if(l<r)
{
for(int i=;i<initial.size();i++)
{
if(a[i]!=b[i+l])
break;
if(i==initial.size()-)
flag=;
}
}
else
{
for(int i=;i<initial.size();i++)
{
if(a[i]!=b[r-i])
break;
if(i==initial.size()-)
flag=;
}
}
if(flag)
return "Possible";
else
return "Impossible";
}
};

TC SRM 663 div2 B AABB 逆推的更多相关文章

  1. TC SRM 663 div2 A ChessFloor 暴力

    ChessFloor Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description Samantha is renovating a squa ...

  2. TC SRM 665 DIV2 A LuckyXor 暴力

    LuckyXorTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description A lucky number is a positive int ...

  3. TC SRM 593 DIV2 1000

    很棒的DP,不过没想出,看题解了..思维很重要. #include <iostream> #include <cstdio> #include <cstring> ...

  4. TC SRM 591 DIV2 1000

    很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...

  5. tc srm 636 div2 500

    100的数据直接暴力就行,想多了... ac的代码: #include <iostream> #include <cstdio> #include <cstring> ...

  6. TC SRM 664 div2 B BearPlaysDiv2 bfs

    BearPlaysDiv2 Problem Statement    Limak is a little bear who loves to play. Today he is playing by ...

  7. TC SRM 664 div2 A BearCheats 暴力

     BearCheats Problem Statement    Limak is an old brown bear. Because of his bad eyesight he sometime ...

  8. TC SRM 665 DIV2 B LuckyCycle 暴力

    LuckyCycleTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...

  9. TC SRM 607 DIV2

    求拼接完成后的字符串包含的子回文串的数目,一开始还用暴力去做,想都不用想 肯定超时了. 复习了一下求最长子回文串的算法,发现可以类似解决. 给相邻字符之间添加一个'@'字符,这样所有的回文串都是奇数长 ...

随机推荐

  1. 【大数取模】HDOJ-1134、CODEUP-1086

    1086: 大数取模   题目描述 现给你两个正整数A和B,请你计算A mod B.为了使问题简单,保证B小于100000. 输入 输入包含多组测试数据.每行输入包含两个正整数A和B.A的长度不超过1 ...

  2. MyGeneration 数据库驱动为空

    重做系统后安装了MyGeneration  但是安装完成后发现驱动中没有驱动信息,查了下网上的解决方案发下下面的可以完美解决,特作备忘: The problem is because of multi ...

  3. HDU-4861 Couple doubi

    http://acm.hdu.edu.cn/showproblem.php?pid=4861 Couple doubi Time Limit: 2000/1000 MS (Java/Others)   ...

  4. QC开发只能修改指派给自己的缺陷,而其他的bug可以查看但是不允许修改

    今天在QC9.0项目中增加了几个项目,然后我的想法是:开发只能修改指派给自己的缺陷,而其他的bug可以查看但是不允许修改 虽说qc我还是比较熟悉的,但是对于这个问题,感觉可能要用到脚本,对于脚本我一窍 ...

  5. duilib中控件拖拽功能的实现方法(附源码)

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41144283 duilib库中原本没有显示的对控件增加拖拽的功能,而实际 ...

  6. JS单例设计模式

     单例,指的是只有一个实例的对象.    在应用单例模式时,生成单例的类必须保证只有一个实例的存在,很多时候整个系统只需要拥有一个全局对象,才有利于协调系统整体的行为.比如在整个系统的配置文件中,配置 ...

  7. thread.join函数,java多线程中的join函数解析

    join函数的作用,是让当前线程等待,直到调用join()的 线程结束或者等到一段时间,我们来看以下代码 package mian; public class simpleplela { static ...

  8. JavaScript相关图书推荐

    JavaScript语言精粹(修订版) 作      者 Douglas Crockford(道格拉斯·克罗克福德) 著:赵泽欣 等 译 出 版 社 电子工业出版社 出版时间 2012-09-01 版 ...

  9. Bias/variance tradeoff

    线性回归中有欠拟合与过拟合,例如下图: 则会形成欠拟合, 则会形成过拟合. 尽管五次多项式会精确的预测训练集中的样本点,但在预测训练集中没有的数据,则不能很好的预测,也就是说有较大的泛化误差,上面的右 ...

  10. TCPSocket v1.0 for cocos2d-x下载

    下载地址:http://files.cnblogs.com/elephant-x/TCPSocketLibs_V1.0.rar 这是自己封装的一个TCPSOCKET包,是独立于cocos2d-x的,使 ...