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)ヽ(=^・ω・^=)丿

猜你喜欢

转载自blog.csdn.net/colpac/article/details/79938597