-
<yellow>aaa<blue>bbb</blue>ccc</yellow>dddd<red>abc</red>
样例输出
-
3 6 3
-
解题思路:栈的模拟
#include<iostream> #include<cstdio> #include<cstring> #include<stack> using namespace std; const int maxn = 1000; char str[maxn]; stack<char> s; //stack<string> s; int main() { bool start; string t; int yellow,red,blue; while(gets(str) != NULL) { if(str[0] == 0) break; while(!s.empty()) s.pop(); //清空栈 yellow = red = blue = 0; start = false; int len = strlen(str); for(int i = 0; i < len; i++) { if(start == false) { if(str[i] == '<') start = true; continue; } if(str[i] == '<' || str[i] == ' ') continue; if(str[i-1] == '<') { if(str[i] == 'y') { s.push('Y'); i += 6; } else if(str[i] == 'b') { s.push('B'); i += 4; } else if(str[i] == 'r') { s.push('R'); i += 3; } else //出现</color>的情况,要退栈。。 { int cnt = 0; //计算出弹出元素个数 while(s.top() >= 'Z') { cnt++, s.pop(); } if(s.top() == 'R') { red += cnt; i += 4; } else if(s.top() == 'B') { blue += cnt; i += 5; } else { yellow += cnt; i += 7; } s.pop(); } } else if(!s.empty()) //此时栈内非空,说明该元素任属于某一种颜色。。 { s.push(str[i]); } } printf("%d %d %d\n",red,yellow,blue); } return 0; }
描述
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.