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";
}
};
TC SRM 663 div2 B AABB 逆推的更多相关文章
- TC SRM 663 div2 A ChessFloor 暴力
ChessFloor Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description Samantha is renovating a squa ...
- TC SRM 665 DIV2 A LuckyXor 暴力
LuckyXorTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description A lucky number is a positive int ...
- TC SRM 593 DIV2 1000
很棒的DP,不过没想出,看题解了..思维很重要. #include <iostream> #include <cstdio> #include <cstring> ...
- TC SRM 591 DIV2 1000
很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...
- tc srm 636 div2 500
100的数据直接暴力就行,想多了... ac的代码: #include <iostream> #include <cstdio> #include <cstring> ...
- TC SRM 664 div2 B BearPlaysDiv2 bfs
BearPlaysDiv2 Problem Statement Limak is a little bear who loves to play. Today he is playing by ...
- TC SRM 664 div2 A BearCheats 暴力
BearCheats Problem Statement Limak is an old brown bear. Because of his bad eyesight he sometime ...
- TC SRM 665 DIV2 B LuckyCycle 暴力
LuckyCycleTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...
- TC SRM 607 DIV2
求拼接完成后的字符串包含的子回文串的数目,一开始还用暴力去做,想都不用想 肯定超时了. 复习了一下求最长子回文串的算法,发现可以类似解决. 给相邻字符之间添加一个'@'字符,这样所有的回文串都是奇数长 ...
随机推荐
- 也谈http中get和post
1.get和post区别: 从设计初衷考虑get是为了查询服务器资源(不改变服务器数据及状态,因此说它是安全和幂等的,但get请求参数一般是直接在url后面,浏览器地址栏中会被看到能保存书签及历史记录 ...
- function 中定义函数的默认返回值,
result有默认值的int类型的为0,string类型的为‘’,tobject类型的为nil等等
- Android 版本自动更新
截图如下: 代码实现如下: package com.update.apk; import java.io.BufferedReader; import java.io.File; import jav ...
- UITextField限制字数的方法
转:http://blog.csdn.net/marujunyy/article/details/9985411 在输入东西的时候,如果想限制最大字数,可以用下面方法: - (BOOL)textFie ...
- 值班问题:insert语句插入了两条数据?
上周值班,碰到这样的一个客户问题,表结构简化如下: CREATE TABLE `aa` (`c1` int(10) unsigned NOT NULL AUTO_INCREMENT,`c2` int( ...
- 那些经常被遗忘的 Java 面试题
静态类和静态方法 如果一个类要被声明为static的,只有一种情况,就是静态内部类. 静态内部类实际上与普通类(即类名必须与文件名一样的顶级类)一样,只是静态内部类在某一类的内部定义了而已,既然是类, ...
- JVM内存结构
前言 在Java语言开发过程中,out of memory错误是很常见的一种错误.对于JVM的内存结构有更深入的了解,更更好的帮我们排查此类问题,有效的避免此类问题发生.在JAVA 8中内存结构有进行 ...
- Java 任务调度
1.普通方式 /** * 普通thread * 这是最常见的,创建一个thread,然后让它在while循环里一直运行着, * 通过sleep方法来达到定时任务的效果.这样可以快速简单的实现 */ p ...
- struts2+Hibernate4+spring3+EasyUI环境搭建之三:引入sututs2以及spring与sututs2整合
1.引入struts2 <!-- struts2 和心包 排除javassist 因为hibernate也有 会发生冲突--> <dependency> <groupId ...
- 使用Core Data应避免的十个错误
原文:Avoiding Ten Big Mistakes iOS Developers Make with Core Data http://www.cocoachina.com/applenew ...