Colorful Lecture Note(手工栈)
题目1 : 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
注意:需要用gets来读空格。#include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> #include <ctime> #include <cmath> #include <string> #include <cstring> #include <stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> using namespace std; const int INF=0x3f3f3f3f; const double eps=1e-10; const double PI=acos(-1.0); #define maxn 1100 struct Sta { char word[maxn]; int cnt = 0; }; Sta sta[100]; char a[1200]; int main() { gets(a); { int k = 1; int cnt1 = 0; int cnt2 = 0; int cnt3 = 0; int j; for(int i = 0; a[i] != '\0'; i++) { if(a[i] == ' ') continue; int k1 = 0; int flag = 0; if(a[i]=='<') { k++; for(j = i+1; a[j] != '>'; j++) { if(a[j]=='/') flag = 1; sta[k].word[k1++] = a[j]; } sta[k].word[k1] = '\0'; i=j; if(flag) { if(strcmp(sta[k-1].word,"yellow")== 0) cnt1 += sta[k-1].cnt; if(strcmp(sta[k-1].word,"blue")== 0) cnt2 += sta[k-1].cnt; if(strcmp(sta[k-1].word,"red")== 0) cnt3 += sta[k-1].cnt; sta[k-1].cnt = sta[k].cnt = 0; k=k-2; } continue; } sta[k].cnt++; } printf("%d %d %d\n", cnt3,cnt1,cnt2); } return 0; } /*<yellow>aaa<blue>bbb </blue>ccc</yellow>dddd<red>abc</red>*/