STring (AtCoder Grand Contest 005) (c++) (栈)

问题 I: STring

时间限制: 1 Sec   内存限制: 128 MB
提交: 221   解决: 76
[提交][状态][讨论版][命题人:admin]

题目描述

We have a string X, which has an even number of characters. Half the characters are 'S', and the other half are 'T'.

Takahashi, who hates the string 'ST', will perform the following operation 1010000 times:

Among the occurrences of 'ST' in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.
Find the eventual length of X.

Constraints
2≦|X|≦200,000
The length of X is even.
Half the characters in X are 'S', and the other half are 'T'.

Partial Scores
In test cases worth 200 points, |X|≦200.

输入

The input is given from Standard Input in the following format:
X

输出

Print the eventual length of X.

样例输入

TSTTSS

样例输出

4

提示

In the 1-st operation, the 2-nd and 3-rd characters of 'TSTTSS' are removed. X becomes 'TTSS', and since it does not contain 'ST' anymore, nothing is done in the remaining 1010000−1 operations. Thus, the answer is 4.



很简单的一个签到题,题目大意就是给定一个长度小于200000的字符串,如果串中含有"ST"串,就删除最左边的那个,进行10^10000次删除操作,如果整个串中不含"ST"串,不进行任何操作。输出删除全部"ST"串后的串长度。


明显可以用栈解决这个问题,以下是代码(当然参考了别人的)(因为我队想复杂了)o(*▽*)q

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;

int main(){
    string str ;
    while ( cin >> str ){
        int len = str.size() ;
        stack<char> container ;
        for ( int i = 0 ; i < str.size() ; i ++ ){
            if ( str[i] == 'T' ){
                if ( !container.empty() ){
                    if ( container.top() == 'S' ){
                        len -= 2 ;
                        container.pop() ;
                    }
                }else{
                    container.push(str[i]) ;
                }
            }else{
                container.push(str[i]) ;
            }
        }
        cout << len << endl ;
    }
    return 0 ;
}


以下是我队代码:

#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std ;

int ans( char a[], int index, bool check[] ){
    int k = 0 ;
    for(int i=0; i<index; i++){
        if(check[i]==false){
            index --;
        }
    }
    for ( int i = 0 ;  ; i ++, k ++ ){
        if ( check[i] == false ){
            k -- ;
            continue ;
        }
        if ( k == index ){
            return i;
        }
    }
}

int main()
{
    char str[200005] ;
    bool check[200005] ;
    while ( cin >> str ){
        int sum=0;
        for(int i=0; str[i]!='\0'; i++){
            sum++;
        }
        memset( check, true, sizeof( check ) ) ;
        bool flag = 0;
        for(;;){
            flag = 1;
            for(int i=0; str[i]!='\0'; i++){
                for(int j=0; str[i-j]=='S'&&str[i+1+j]=='T'; j++){
                    flag = 0;
                    check[i-j] = 0;
                    check[i+j+1] = 0;
                }
            }
            bool flag2 =  1;
            for(int i=0; str[i]!='\0'; i++){
                if(check[i]==1){
                    flag2 = 0;
                    break;
                }
            }
            if(flag2){
                break;
            }
            if(flag){
                break;
            }
            else{
                int t = 0;
                for(int i=0; i==0||str[i-1]!='\0'; i++){
                    if(check[i]!=0){
                        str[t++] = str[i];
                    }
                }
                for(int i=0; i<t; i++){
                    check[i] = 1;
                }
            }
        }
        int ans = 0 ;
        for ( int i = 0 ; str[i]!='\0' ; i ++ ){
            if ( check[i] == true ){

                ans ++ ;
            }
        }
        cout << ans << endl ;
    }
    return 0 ;
}
(明显做复杂了的节奏,想法忘了....)
(用栈解决简单粗暴)

(日常膜拜dalao)ヽ(=^・ω・^=)丿

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值