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
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
<yellow>aaa<blue>bbb</blue>ccc</yellow>dddd<red>abc</red>
Sample Output
3 6 3 题意:统计 yellow、red、blue 标签中字母个数
与括号序列类似,只不过此处的括号为标签
#include <iostream>
#include <cstdio>
#include <stack>
#include <cctype>
#include <cstring>
using namespace std;
// <yellow>aaa<blue>bbb</blue>ccc</yellow>dddd<red>abc</red> int main(int argc, char const *argv[])
{
char input[];
while(gets(input)){
stack<char> s;
int r_count,y_count,b_count;
r_count = y_count = b_count = ;
for (int i = ; i < strlen(input); ++i)
{
if(input[i] == '<'){
if(input[i+] == 'y'){
s.push('y');
i += ;
continue;
}else if(input[i+] == 'b'){
s.push('b');
i += ;
continue;
}else if(input[i+] == 'r'){
s.push('r');
i += ;
continue;
}else if(input[i+] == '/'){
s.pop();
if(input[i+] == 'y'){
i += ;
continue;
}else if(input[i+] == 'b'){
i += ;
continue;
}else if(input[i+] == 'r'){
i += ;
}
}
}else if(!s.empty()){
if(s.top() == 'r' && isalpha(input[i])){
r_count++;
}else if(s.top() == 'y' && isalpha(input[i])){
y_count++;
}else if(s.top() == 'b' && isalpha(input[i])){
b_count++;
}
}
}
cout << r_count << " " << y_count << " " << b_count << endl;
}
return ;
}