ABBADiv1

题意:

规定两种操作,一种是在字符串的末尾添加A,另一种是在末尾添加B然后反转字符串。现在给你一个起始串,一个终点串,然后问你是否能够通过以上两种操作,从起始串变为终点串。

题解:

将问题反过来考虑,那么问题就变为了是否能够从终点串变为起始串。令起始串为s,终点串为t。

首先考虑串t就是串s的子串,那么这个子串的前面的B的数量一定要和这个子串后面的B的数量相同,这是因为,只有相同的时候才能消掉。并且如果第一字符不是B,且匹配的位置不是在第一个,那么第一次的反转就无法成功,即s前面的那些A是消不掉的。

第二种情况就是t的反转时s的子串,做法和前面相似,只是判断条件变为了子串前面的B的数量要比后面的B的数量小1。

代码:

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
using namespace std; class ABBADiv1 {
public:
string canObtain(string initial, string target) {
string s = initial, t = target;
int pos = -;
while (true) {
pos = t.find(s, pos + );
if (pos == t.npos)break;
int B0 = , B1 = ;
for (int i = ; i < pos; i++)if (t[i] == 'B')B0++;
for (int i = pos + s.length(); i < t.length(); i++)if (t[i] == 'B')B1++;
if (B0 == B1) {
if (pos == )return "Possible";
else if (t[] == 'B')return "Possible";
}
}
reverse(s.begin(), s.end());
pos = -;
while (true) {
pos = t.find(s, pos + );
if (pos == t.npos)break;
int B0 = , B1 = ;
for (int i = ; i < pos; i++)if (t[i] == 'B')B0++;
for (int i = pos + s.length(); i < t.length(); i++)if (t[i] == 'B')B1++;
if (B0 == B1 + && t[] == 'B')
return "Possible";
}
return "Impossible";
}
};

Topcoder SRM 663 DIV 1的更多相关文章

  1. TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E

    传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...

  2. TopCoder SRM 667 Div.2题解

    概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...

  3. [topcoder]SRM 646 DIV 2

    第一题:K等于1或者2,非常简单.略.K更多的情况,http://www.cnblogs.com/lautsie/p/4242975.html,值得思考. 第二题:http://www.cnblogs ...

  4. [topcoder]SRM 633 DIV 2

    第一题,http://community.topcoder.com/stat?c=problem_statement&pm=13462&rd=16076 模拟就可以了. #includ ...

  5. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  6. Topcoder SRM 656 (Div.1) 250 RandomPancakeStack - 概率+记忆化搜索

    最近连续三次TC爆零了,,,我的心好痛. 不知怎么想的,这题把题意理解成,第一次选择j,第二次选择i后,只能从1~i-1.i+1~j找,其实还可以从j+1~n中找,只要没有被选中过就行... [题意] ...

  7. Topcoder SRM 648 (div.2)

    第一次做TC全部通过,截图纪念一下. 终于蓝了一次,也是TC上第一次变成蓝名,下次就要做Div.1了,希望div1不要挂零..._(:зゝ∠)_ A. KitayutaMart2 万年不变的水题. # ...

  8. topcoder srm 663 div1

    problem1 link 每次枚举$S$的两种变化,并判断新的串是否是$T$的子串.不是的话停止搜索. problem2 link 首先考慮增加1个面值为1的硬币后,$ways$数组有什么变化.设原 ...

  9. 【topcoder SRM 702 DIV 2 250】TestTaking

    Problem Statement Recently, Alice had to take a test. The test consisted of a sequence of true/false ...

随机推荐

  1. Compoer介绍

    Compoer介绍 Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. 安装Composer Composer.phar 是 Compos ...

  2. 【bzoj3339】Rmq Problem

    [bzoj3339]Rmq Problem   Description Input Output Sample Input 7 50 2 1 0 1 3 21 32 31 43 62 7 Sample ...

  3. loj2073 「JSOI2016」扭动的回文串

    ref 主要是要理解"撑到"最长这个概念 (为啥我的代码这么长QAQ #include <iostream> #include <cstdio> using ...

  4. 【Reverse Nodes in k-Group】cpp

    题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...

  5. leetcode 【 Find Minimum in Rotated Sorted Array II 】python 实现

    题目: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? W ...

  6. 精通CSS高级Web标准解决方案(3-1 背景图像与图像替换)

    3.1背景图像基础 3.2图像替换 使用文本的图像并保留文本的方法.

  7. 基于Jquery的商城商品图片的放大镜效果(非组件)

    在开发商城的时候,往往会用到图片的放大功能,这里把自己在近期项目中使用的放大镜特效做一下总结(非插件). 放大镜效果 常用的js组件jquery.imagezoom,jquery.jqzoom,jqu ...

  8. 【转】Unity3D学习日记(二)使用UGUI制作虚拟摇杆控制摄像机

    http://blog.csdn.net/begonia__z/article/details/51178907 前天撸了一个简单的UGUI虚拟摇杆,今天我就利用前天做的虚拟摇杆做了一个简单的摄像机控 ...

  9. Func<T, TResult> 委托

    Func<T, TResult> 委托 Visual Studio 2008   命名空间:  System程序集:  System.Core(在 System.Core.dll 中) 语 ...

  10. 【bzoj2280】[Poi2011]Plot 二分+倍增+二分+最小圆覆盖

    题目描述 给出一系列点p_1, p_2, ... , p_n,将其分成不多余m个连续的段,第i段内求一个点q_i,使得q_i到这段内点的距离的最大值的最大值最小 输入 第一行,n m下面n行,每行两个 ...