码迷,mamicode.com
首页 > 其他好文 > 详细

hihoCoder - 1103 - Colorful Lecture Note (栈~~)

时间:2015-02-07 09:10:05      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:acm   hihocoder      

#1103 : Colorful Lecture Note

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

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


没看清题目,,这个字符串中可以有空格,,晕死~~~害得我无情的WA了一次,,还让我检查半天技术分享


分析:就是利用栈来算出每种颜色字母的个数


AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;

char a[1005];

int main() {
	while(gets(a) != NULL) {
		int red = 0, yellow = 0, blue = 0;
		int len = strlen(a);
		stack<int> s;
		for(int i = 0; i < len; i++) {
			if(a[i] == '<') {
				if(a[i + 1] == 'r') {
					s.push(1); i += 4; continue;
				} else if(a[i + 1] == 'y') {
					s.push(2); i += 7; continue;
				} else if(a[i + 1] == 'b') {
					s.push(3); i += 5; continue;
				} else if(a[i + 1] == '/') {
					s.pop();
					if(a[i + 2] == 'r') {
						i += 5; continue;
					} else if(a[i + 2] == 'y') {
						i += 8; continue;
					} else if(a[i + 2] == 'b') {
						i += 6; continue;
					}
				}
			} else {
				if(s.empty()) continue;
				else if(s.top() == 1 && (a[i] <= 'z' && a[i] >= 'a')) red++;
				else if(s.top() == 2 && (a[i] <= 'z' && a[i] >= 'a')) yellow++;
				else if(s.top() == 3 && (a[i] <= 'z' && a[i] >= 'a')) blue++;
			}
		}
		printf("%d %d %d\n", red, yellow, blue);
	}
	return 0;
} 









hihoCoder - 1103 - Colorful Lecture Note (栈~~)

标签:acm   hihocoder      

原文地址:http://blog.csdn.net/u014355480/article/details/43581149

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!