【TC 695_600】BearPasswordAny(模拟)

【TC 695_600】BearPasswordAny(模拟)

Problem Statement

 

A substring of a string is any non-empty contiguous subsequence of its characters. For example, both "abc" and "bcd" are substrings of "abcde", but "ace" is not a substring of "abcde".


A string is called constant if all of its characters are the same. For example, "a" and "bbbbb" are constant strings, but "aba" is not a constant string.


Two substrings of the same string are considered distinct if they start or end at a different position. For example, the string "ababab" contains three distinct copies of the substring "ab", and the string "aaaa" contains two distinct copies of the substring "aaa".


Bear Limak is creating a new account and he needs to choose a password. His password should satisfy the following security requirements:

  • The password must be a string of length N.
  • Each character of the password must be either 'a' or 'b'.
  • For each i between 1 and N, inclusive, the password must contain exactly x[i-1] constant substrings of length i.

You are given the vector <int> x with N elements. Help Limak: find and return a valid password! If there are many valid passwords, you may return any of them. If there are no valid passwords, return "" (i.e., an empty string).

Definition

 
Class:BearPasswordAny
Method:findPassword
Parameters:vector <int>
Returns:string
Method signature:string findPassword(vector <int> x)
(be sure your method is public)

Limits

 
Time limit (s):2.000
Memory limit (MB):256
Stack limit (MB):256

Constraints

-x will contain between 1 and 50, inclusive.
-Each element of x must be between 0 and N, inclusive (where N = length(x))

Examples

0) 
 
{5,0,0,0,0}
Returns: "ababa"
Since the given vector <int> x has five elements, the password must contain exactly five characters. A password must contain x[0] = 5 constant substrings of length 1, and 0 constant substrings of bigger lengths. The only two valid passwords are "ababa" and "babab". You may return any of them.
1) 
 
{4,2,1,0}
Returns: "baaa"
2) 
 
{3,1,1}
Returns: ""
3) 
 
{4,3,2,1}
Returns: "aaaa"
4) 
 
{0}
Returns: ""
5) 
 
{4,0,3,2}
Returns: ""
6) 
 
{6,3,1,0,0,0}
Returns: "abbaaa"
7) 
 
{10,5,2,1,0,0,0,0,0,0}
Returns: "bbbbaababb"
8) 
 
{5,4,2,1,0}
Returns: ""

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     


题目大意:

已知一串密码仅由a和b组成,给出一个vector <int> a;

a[i]表示i+1个连续的相同字母的子串有a[i]个(0 <= i < n)

类似aabaaa

a[0] = 6

a[1] = 3

a[2] = 1

a[3]~a[5] = 0

赛时用搜。。TLE一组。。

赛后改了几种姿势,都是TLE一组。只是TLE的组变了=。=

瞬间感觉TC的数据好硬。。


正解应该是从大串往小串的数量推。

当出现j个长为i的串时,则就有了j+1个长为i-1的串,j+2个i-2。。。

这样模拟加串即可。。。当出现一些非法数据,比如a[i] < 0或者最终结果串长<n或者>n(vector大小)就返回空串


代码如下:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <map>
#include <stack>
#include <algorithm>
#include <cmath>
#define LL long long
#define Pr pair<int,int>
#define VI vector<int>

using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int msz = 1e5+1;

//class
class BearPasswordAny
{
public:
    //
    char ans[55];

    bool solve(vector <int> x)
    {
        int pos = 0;
        char ch = 'a';
        int n = x.size();
        for(int i = n-1; i >= 0; --i)
        {
			while(x[i])
			{
           		for(int k = 0; k <= i; ++k) 
           		{
           		    ans[pos++] = ch;
           		    if(pos > n) return false;
           		}
           		ch = (ch == 'a')? 'b': 'a';

           		int tmp = 1;
           		for(int j = i; j >= 0; --j)
           		{
           		    x[j] -= tmp;
           		    if(x[j] < 0) return false;
           		    tmp++;
           		}
			}
        }
        if(pos == n) return true;
        return false;
    }

    //function
    string findPassword(vector <int> x)
    {
        if(!solve(x)) return "";
        ans[x.size()] = 0;
        return ans;
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值