hihocoder 1103 Colorful Lecture Note(字符串處理)

Little Hi is writing an algorithm lecture note for Little Ho. To make the note more comprehensible, Little Hi tries to color some of the text. Unfortunately Little Hi is using a plain(black and white) text editor. So he decides to tag the text which should be colored for now and color them later when he has a more powerful software such as Microsoft Word.

There are only lowercase letters and spaces in the lecture text. To mark the color of a piece of text, Little Hi add a pair of tags surrounding the text, <COLOR> at the beginning and </COLOR> at the end where COLOR is one of "red", "yellow" or "blue".

Two tag pairs may be overlapped only if one pair is completely inside the other pair. Text is colored by the nearest surrounding tag. For example, Little Hi would not write something like "<blue>aaa<yellow>bbb</blue>ccc</yellow>". However "<yellow>aaa<blue>bbb</blue>ccc</yellow>" is possible and "bbb" is colored blue.

Given such a text, you need to calculate how many letters(spaces are not counted) are colored red, yellow and blue.

輸入

Input contains one line: the text with color tags. The length is no more than 1000 characters.

輸出

Output three numbers count_red, count_yellow, count_blue, indicating the numbers of characters colored by red, yellow and blue.

樣例輸入
<yellow>aaa<blue>bbb</blue>ccc</yellow>dddd<red>abc</red>
樣例輸出

3 6 3

分析:上次去地大的題目,搞了半天,原來模擬做WA了半天的,現在想了一下,由於要找最近的

所以用個棧維護下就行了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
stack<char>S;
char str[1100];
int main()
{
    while(gets(str))
    {
        while(!S.empty()) S.pop();
        int len=strlen(str);
        int y=0,b=0,r=0;
        //cout<<"fuck  "<<len<<endl;
        for(int i=0;i<len;i++)
        {
            //cout<<"hahah  "<<i<<" "<<str[i]<<endl;
            if(str[i]=='<')
            {
                if(str[i+1]=='y')
                {
                    S.push('y');
                    i+=7;
                }
                else if(str[i+1]=='b')
                {
                    S.push('b');
                    i+=5;
                }
                else if(str[i+1]=='r')
                {
                    S.push('r');
                    i+=4;
                }
                else if(str[i+1]=='/')
                {
                    S.pop();
                    if(str[i+2]=='y')
                        i+=8;
                    else if(str[i+2]=='b')
                        i+=6;
                    else if(str[i+2]=='r')
                        i+=5;
                }
            }
            else
            {
                if(S.empty())  continue;
                if(S.top()=='y'&&isalpha(str[i]))
                    y++;
                else if(S.top()=='b'&&isalpha(str[i]))
                    b++;
                else if(S.top()=='r'&&isalpha(str[i]))
                    r++;
            }
        }
        printf("%d %d %d\n",r,y,b);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章