hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)

#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

题目分析:字符串处理+栈,假设当前出现了<yellow>就让代表yellow的字母y进栈,表示当前处于yellow状态。
栈顶元素代表是什么,就表示当前字符是什么颜色的。如果遇到</blue>或</yellow>或</red>,就让代表其颜色
字符从栈顶出栈。
代码:
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stack>

using namespace std;

int main()
{
    char s[1010];
    int i, j, len;
    int y=0, b=0, r=0;
    gets(s); //需要用gets读取,因为原字符串可能会包含空格,第一次用scanf就WA了,2Y
    len=strlen(s);
    stack<char>sta;
    for(i=0; i<len; )
    {
        if(s[i]=='<')
        {
            if(s[i+1]=='y')
            {
                sta.push('y'); i+=8; //i+8的目的是跳过一些字符,因为已经知道是yellow了,
                                    //没必要继续往下挨着比对了
            }
            else if(s[i+1]=='b')
            {
                sta.push('b'); i+=6;
            }
            else if(s[i+1]=='r')
            {
                sta.push('r'); i+=5;
            }
            else if(s[i+1]=='/' )
            {
                sta.pop();
                if(s[i+2]=='y')
                    i+=9;
                else if(s[i+2]=='b')
                    i+=7;
                else if(s[i+2]=='r')
                    i+=6;
            }
        }
        else if(isalpha(s[i]))
        {
            if(!sta.empty())
                while( isalpha(s[i])&&i<len )
                {
                    if(sta.top()=='y')
                        y++;
                    else if(sta.top()=='b')
                        b++;
                    else
                        r++;
                    i++;
                }
            else
                i++;
        }
        else
            i++;
    }
    printf("%d %d %d
", r, y, b);
    return 0;
}

原文地址:https://www.cnblogs.com/yspworld/p/4261501.html