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.ios
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".app
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.ide
Given such a text, you need to calculate how many letters(spaces are not counted) are colored red, yellow and blue.spa
InputInput contains one line: the text with color tags. The length is no more than 1000 characters.rest
OutputOutput three numbers count_red, count_yellow, count_blue, indicating the numbers of characters colored by red, yellow and blue.code
Sample Inputblog
<yellow>aaa<blue>bbb</blue>ccc</yellow>dddd<red>abc</red>
Sample Outputthree
3 6 3
題意:統計 yellow、red、blue 標籤中字母個數
與括號序列相似,只不過此處的括號爲標籤
1 #include <iostream> 2 #include <cstdio> 3 #include <stack> 4 #include <cctype> 5 #include <cstring> 6 using namespace std; 7 // <yellow>aaa<blue>bbb</blue>ccc</yellow>dddd<red>abc</red> 8 9 int main(int argc, char const *argv[]) 10 { 11 char input[1010]; 12 while(gets(input)){ 13 stack<char> s; 14 int r_count,y_count,b_count; 15 r_count = y_count = b_count = 0; 16 for (int i = 0; i < strlen(input); ++i) 17 { 18 if(input[i] == '<'){ 19 if(input[i+1] == 'y'){ 20 s.push('y'); 21 i += 7; 22 continue; 23 }else if(input[i+1] == 'b'){ 24 s.push('b'); 25 i += 5; 26 continue; 27 }else if(input[i+1] == 'r'){ 28 s.push('r'); 29 i += 4; 30 continue; 31 }else if(input[i+1] == '/'){ 32 s.pop(); 33 if(input[i+2] == 'y'){ 34 i += 8; 35 continue; 36 }else if(input[i+2] == 'b'){ 37 i += 6; 38 continue; 39 }else if(input[i+2] == 'r'){ 40 i += 5; 41 } 42 } 43 }else if(!s.empty()){ 44 if(s.top() == 'r' && isalpha(input[i])){ 45 r_count++; 46 }else if(s.top() == 'y' && isalpha(input[i])){ 47 y_count++; 48 }else if(s.top() == 'b' && isalpha(input[i])){ 49 b_count++; 50 } 51 } 52 } 53 cout << r_count << " " << y_count << " " << b_count << endl; 54 } 55 return 0; 56 }