微软苏州校招笔试题目(1月10日)Colorful Lecture Note的解法

题目描述 : 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


解法思路:

首先从右搜索字符串中开头标记字符串所处的位置,如果存在则从该位置以起始点搜索对应的结尾标记字符串所在的位置。此处,如果这两个位置中间的子字符串如果不存在其他标记字符串,则停止此处的搜索循环,并记录此对应标记字符串中的字符个数;原字符串中剩余的所有子字符串则再进入下一个搜索循环。否则这两个位置中间的子字符串,以及原字符串中剩余的所有子字符串都再进入下一个搜索循环。


解法完整代码如下:

//
//  main.cpp
//  ColorfulLectureNote
//

#include <iostream>
#include <string>
using namespace std;

const string strStartColors[] = { "<red>", "<yellow>", "<blue>" };
const string strEndColors[] = { "</red>", "</yellow>", "</blue>" };
int countOfColors[] = {0, 0, 0};

class Solution{
public: 
    string removeAllWhiteSpaces(string &str)
    {
        string newStr;
        for(int i=0; i<str.size(); i++)
        {
            if(str[i]!=' ')
                newStr+=str[i];
        }
        return newStr;
    }

    void getNumberOfColorfulLectureNote(string &str){
        string newStr = removeAllWhiteSpaces(str);
        countOfColors[0] = 0;
        countOfColors[1] = 0;
        countOfColors[2] = 0;
        getNumberOfColorfulLectureNote(0, newStr);
    }
    
    void getNumberOfColorfulLectureNote(int colorIndex, string &str){
        if(str.size()<=0)
            return;
        int colorIndexLeft = (colorIndex - 1 + 3)%3;
        int colorIndexRight = (colorIndex + 1)%3;
        size_t rspos = str.rfind(strStartColors[colorIndex]);
        size_t repos;
        if (rspos!=string::npos) {
            repos = str.find(strEndColors[colorIndex],rspos);
            if (repos!=string::npos) {
                string newStr1 = str.substr(rspos, repos-rspos+strEndColors[colorIndex].size());
                size_t yspos = newStr1.find(strStartColors[colorIndexLeft]);
                size_t bspos = newStr1.find(strStartColors[colorIndexRight]);
                if (yspos==string::npos&&bspos==string::npos) {
                    countOfColors[colorIndex] += repos-rspos-strStartColors[colorIndex].size();
                } else{
                    if (yspos!=string::npos) {
                        getNumberOfColorfulLectureNote(colorIndexLeft,newStr1);
                    } else {
                        getNumberOfColorfulLectureNote(colorIndexRight,newStr1);
                    }
                }
                
                string newStr2 = str.substr(0,rspos)+str.substr(repos+strEndColors[colorIndex].size());
                getNumberOfColorfulLectureNote(colorIndex, newStr2);
            } else {
                exit(-1);
            }
        } else {
            size_t yspos = str.find(strStartColors[colorIndexLeft]);
            size_t bspos = str.find(strStartColors[colorIndexRight]);
            if (yspos==string::npos&&bspos==string::npos) {
                return;
            } else{
                if (yspos!=string::npos) {
                    getNumberOfColorfulLectureNote(colorIndexLeft,str);
                } else {
                    getNumberOfColorfulLectureNote(colorIndexRight,str);
                }
            }
        }
    }
};

int main(int argc, const char * argv[]) {
    string testStr = "<yellow>aaa<blue>bbb</blue>ccc</yellow>dddd<red>abc</red>";
    Solution s;
    s.getNumberOfColorfulLectureNote(testStr);
    std::cout << countOfColors[0] << '\t' << countOfColors[1] << '\t' << countOfColors[2] << endl;
    return 0;
} 

代码中可能有很多不足或错误的地方,欢迎各位大神们提出宝贵意见。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值