HihoCoder1103 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, at the beginning and 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 “aaabbbccc”. However “aaabbbccc” 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
Input contains one line: the text with color tags. The length is no more than 1000 characters.

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

Sample Input
aaabbbcccddddabc

Sample Output
3 6 3

題目大意:在<顏色> 和 </顏色> 之間的字母就是該顏色,可用一個棧來維護,然後模擬算出每個顏色的字符有多少個

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstring>
#include <stack>
using namespace std;
const int MAX = 1009;
typedef long long ll;
stack<char> s;
char a[MAX];
int main()
{
    while(gets(a))
    {
        int r, y, b;
        r = y = b = 0;
        for(int i = 0; a[i] != '\0'; i++)
        {
            if(a[i] == '<')
            {
                if(a[i+1] == 'r')
                {
                    s.push('r');
                    i += 4;
                    continue;
                }
                else if(a[i+1] == 'y')
                {
                    s.push('y');
                    i += 7;
                    continue;
                }
                else if(a[i+1] == 'b')
                {
                    s.push('b');
                    i += 5;
                    continue;
                }
                else if(a[i+1] == '/')
                {
                    s.pop();
                    if(a[i+2] == 'r')
                    {
                        i += 4;
                        continue;
                    }
                    else if(a[i+2] == 'y')
                    {
                        i += 7;
                        continue;
                    }
                    else if(a[i+2] == 'b')
                    {
                        i += 5;
                        continue;
                    }
                }
            }
            else
            {
                if(!s.empty())
                {
                    if(s.top() == 'r' && isalpha(a[i]))
                        r++;
                    if(s.top() == 'y' && isalpha(a[i]))
                        y++;
                    if(s.top() == 'b' && isalpha(a[i]))
                        b++;
                }
            }
        }
        printf("%d %d %d\n",r, y, b);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章