Quantcast
Channel: xdlove
Viewing all articles
Browse latest Browse all 4

微软笔试题《Colorful Lecture Note》

$
0
0

#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.

样例输入
&lt;yellow&gt;aaa&lt;blue&gt;bbb&lt;/blue&gt;ccc&lt;/yellow&gt;dddd&lt;red&gt;abc&lt;/red&gt;
样例输出
3 6 3
题意:题目给你一行字符串,只包含小写字母和空格。一开始字符串被刷上黑白色。现在hi通过<color></color>标签来对字符串进行涂色。题目允许嵌套涂色。以嵌套层涂的颜色为主。
题解:一道简单的字符串处理题目,加上栈的略微应用。

#include <iostream>
#include <vector>
#include <string.h>
#include <string>
#include <stack>
#include <algorithm>

using namespace std;

int countRed, countYellow, countBlue;

enum color
{
    red, yellow, blue
};

void increateCountColor(color ccolor,int num) {
    switch (ccolor)
    {
    case red:
        countRed += num;
        break;
    case yellow:
        countYellow += num;
        break;
    case blue:
        countBlue += num;
        break;
    default:
        break;
    }
}

color findColor(string strColor) {
    if (strColor == "red") {
        return red;
    }
    else if (strColor == "yellow") {
        return yellow;
    }
    else {
        return blue;
    }
}

void outputCountRedYellowAndBlue() {
    stack<color> st;
    string strInput;
    int strLength,countCharacters = 0;
    getline(cin, strInput);
    countRed = countBlue = countYellow = 0;
    strLength = strInput.length();
    for (int i = 0; i < strLength; i++) {
        if (strInput[i] == '<') {
            bool isEndColor = false;
            string strTemp = "";
            ++i;
            while (strInput[i] != '>') {
                if (strInput[i] != '/') {
                    strTemp += strInput[i];
                }
                else {
                    isEndColor = true;
                }
                ++i;
            }
            if (isEndColor == true) {
                increateCountColor(st.top(), countCharacters);
                st.pop();
            }
            else {
                if (st.empty() == false) {
                    increateCountColor(st.top(), countCharacters);
                }
                st.push(findColor(strTemp));
            }
            countCharacters = 0;
        }
        else {
            if(strInput[i] != ' ') countCharacters++;
        }
    }
    cout << countRed << ' ' << countYellow << ' ' << countBlue << endl;
}

int main() {
    outputCountRedYellowAndBlue();
    return 0;
}


Viewing all articles
Browse latest Browse all 4