TC SRM 663 div2 B AABB 逆推
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";
}
};
最新文章
- WPF 四种样式
- WordPress基础:文章的自定义栏目的使用
- [moka同学笔记]yii2.0小物件的简单使用(第二种方法)
- 在提交SVN时有时候会报svn is already locked 错误
- Android DiffUtil
- MSSql2008打开企业管理器出错,具体显示提示无法识别的配置节 system.serviceModel。
- 海蜘蛛网络科技官方网站 :: 做最好的中文软路由 :: 软件路由器 :: 软路由 :: 软件路由 :: RouterOs
- bsh for android : 北京
- Android shape使用详解
- C++——虚函数问题小集
- 拼多多大数据开发工程师SQL实战解析
- 错误解决记录------------rhel安装Mysql软件包依赖 mariadb组件
- JS-函数作用域
- Jquery计算时间戳之间的差值,可返回年,月,日,小时等
- Tomcat虚拟目录设置
- Extract Dataset
- 内核futex的BUG导致程序hang死问题排查
- 静默文件安装安装WebLogic
- 怎么用js设置a标签点击链接改变当前颜色
- 第三周vim入门学习2
热门文章
- Android入门:用HttpClient模拟HTTP的GET和POST请求
- TOAD FOR MYSQL 进行数据插入时乱码的解决办法---MariaDB 5.5
- Python闭包与javascript闭包比较
- codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制
- POJ 2280&;&;hdu 1661
- 如何用chrome修改js代码,跳过网站等待时间
- poj1001 Exponentiation
- HDU ACM Eight
- Emacs和它的朋友们——阅读源代码篇(转)
- 防asp木马运行