Topcoder SRM 663 DIV 1

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 = -1;
        while (true) {
            pos = t.find(s, pos + 1);
            if (pos == t.npos)break;
            int B0 = 0, B1 = 0;
            for (int i = 0; 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 == 0)return "Possible";
                else if (t[0] == ‘B‘)return "Possible";
            }
        }
        reverse(s.begin(), s.end());
        pos = -1;
        while (true) {
            pos = t.find(s, pos + 1);
            if (pos == t.npos)break;
            int B0 = 0, B1 = 0;
            for (int i = 0; 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 + 1 && t[0] == ‘B‘)
                return "Possible";
        }
        return "Impossible";
    }
};

时间: 2024-09-16 10:37:20

Topcoder SRM 663 DIV 1的相关文章

TopCoder SRM 634 Div.2[ABC]

TopCoder SRM 634 Div.2[ABC] ACM 题目地址: TopCoder SRM 634 赛后做的,感觉现场肯定做不出来Orz,简直不能多说. Level One-MountainRanges[水题] 题意: 问序列中有几个完全大于旁边的峰. 分析: 傻逼题,不多说. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: one.cpp * Create Date: 2014-09-26 21:01:23 * Desc

TopCoder SRM 628 DIV 2

250-point problem Problem Statement    Janusz is learning how to play chess. He is using the standard chessboard with 8 rows and 8 columns. Both the rows and the columns are numbered 0 through 7. Thus, we can describe each cell using its two coordina

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

传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相加,含有n个不同变量的式子的最大值. 另外限制了每一个变量的最大最小值R[i]和L[i]和所有变量之和的最大值Max. n<=13 题外话: 刚开始做这道题的时候,感觉意外眼熟? codeforces 839 E(此题的退化版):http://codeforces.com/contest/839/pro

[topcoder]SRM 633 DIV 2

第一题,http://community.topcoder.com/stat?c=problem_statement&pm=13462&rd=16076 模拟就可以了. #include <vector> #include <algorithm> using namespace std; class Target { public: vector <string> draw(int n) { vector<string> result(n,

[topcoder]SRM 646 DIV 2

第一题:K等于1或者2,非常简单.略.K更多的情况,http://www.cnblogs.com/lautsie/p/4242975.html,值得思考. 第二题:http://www.cnblogs.com/lautsie/p/4245242.html BFS和DFS都可以,注意的是,写的时候,可以往que里几个东西一起扔,就不用建立对象了.也可以直接用二维矩阵记录blocked和visited. 剪枝什么的,最基本的是发现其实在步数限制的情况下,棋盘就是有界的了. 第三题:http://ap

Topcoder SRM 648 (div.2)

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

TopCoder SRM 596 DIV 1 250

body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } Problem Statement      You have an array with N elements. Initially, each element is 0. You can perform the following operations: Increment operation:

Topcoder SRM 687 (Div 2) 250.Quorum

Problem Statement   In one organization they have n different committees. The organization has a very large number of employees. Each employee is a member of each committee. Each committee has a quorum: the smallest number of members that have to be

Topcoder SRM 687 (Div 2) 500.Quacking

Problem Statement   Ducks have started mysteriously appearing in your room. All ducks make the same sound: "quack". Each duck makes the sound one or more times, one after another. For example, valid sounds for a single duck are "quack"