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

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

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

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

    #1103 : Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorit ...

  2. 微软将于2019年12月10日终止Windows 10移动支持

    Windows 10 Mobile support is coming to an end in December. Microsoft stopped developing features for ...

  3. hihoCoder 1095 HIHO Drinking Game 微软苏州校招笔试 12月27日

    由game规则可以看出,T越大超出d的可能性越大,对小ho越有利,其实我是通过打表才看出来这个单调性的==. 对T进行二分搜索,[0,K+1],因为如果N=1,那么应该有T=K+1,小ho才可以获胜. ...

  4. 微软实习生招聘笔试题目

    微软实习生招聘笔试题目 您好!以下是本次微软实习生招聘的笔试题目,请仔细阅读并认真答题: 注意:本次测试题目仅限本人阅读,请勿传阅他人.谢谢! 提交材料 1.不超过2页的说明书,包括算法,设计,使用说 ...

  5. 微软对 Windows 10 Mobile 的支持将于12月10日结束

    在 Windows 支持信息的官方 FAQ 页面上显示:对 Windows 10 Mobile 的支持计划将于今年12月10日结束,此后任何用户将不再会获得安全更新.补丁和免费的协助支持等. 为此,微 ...

  6. 【历史上的今天】9 月 10 日:互联网上第一个搜索工具诞生;微软首席架构师出生;马云诞生

    整理 | 王启隆 透过「历史上的今天」,从过去看未来,从现在亦可以改变未来. 今天是 2021 年 9 月 10 日,教师节.中国的第一个教师节诞生于 1985 年的今天,旨在肯定教师为教育事业所做的 ...

  7. 【历史上的今天】6 月 10 日:Apple II 问世;微软收购 GECAD;发明“软件工程”一词的科技先驱出生

    整理 | 王启隆 透过「历史上的今天」,从过去看未来,从现在亦可以改变未来. 今天是 2022 年 6 月 10 日,在 2006 年,国家作出了从 2006 年起每年 6 月的第二个星期六为中国的& ...

  8. 微软宣布Windows 10 21H1版本,将于5月10日推送

    据外媒Windows Latest报道,Windows 10 2021年度第一次大更新--21H1,即将于2021年5月10日全面推送,而版本19043.928可能是该版本的候选版本.毫不奇怪,202 ...

  9. 阿里巴巴大数据竞赛(2014年3月10日到11月)

    大赛简介 阿里巴巴大数据竞赛是阿里巴巴集团主办,在阿里巴巴大数据科研平台--"天池"上开展的,基于天猫海量真实用户的访问数据的推荐算法大赛. 本次比赛的目的是让广大的高校同学在大数 ...

  10. 互联网晚报 | 9月10日 星期五 | 美团启动数字人民币低碳出行试点;vivo X70系列正式发布;旷视科技科创板IPO过会...

    今日看点 ✦ 腾讯:"共同富裕专项计划"500亿元资金首期开始落地 ✦ TCL启动全球生态合作发展战略,在三大领域投入200亿 ✦ vivo X70系列发布:搭载自研影像芯片V1, ...

最新文章

  1. DispatcherServlet之HandlerAdapter的handle
  2. github入门必备概念
  3. 烧写文件系统——韦东山嵌入式Linux学习笔记11
  4. LeetCode 1161. 最大层内元素和(层序遍历)
  5. linux df和du常用命令
  6. 【C语言笔记初级篇】第七章:结构体相关
  7. 以独占方式锁定此配置文件失败.另一个正在运行_加速用例执行最有效的方法,手把手教你如何并行地运行自动化测试...
  8. OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
  9. (十)OpenCV相机标定
  10. Python---装饰器
  11. win10MATLAB如何完全卸载,怎么完全卸载cad_win10彻底卸载cad的图文步骤-系统城
  12. 局域网内用JAVA建立MQTT客户端监听MQTT服务器消息并持久化到数据库
  13. day12摇色子游戏--笔记
  14. Everything+Wox
  15. Jenkins 如何使用 CrumbIssuer 防御 CSRF 攻击
  16. 蜜罐cowrie搭建和部分问题处理
  17. java课前演讲讲什么_课前演讲讲什么比较有创意(逼格)?
  18. JS——正则表达式(超详细)
  19. 如何创业系列1:创业四要素
  20. 【经典】Mybatis百万级高效批量插入

热门文章

  1. matlab解薛定谔方程,定态薛定谔方程的MATLAB求解(一).doc
  2. 百度竞价后台操作技巧
  3. 前端基础从头学——VsCode使用教程+html基础(入门篇)
  4. tp摄像头的默认地址_TP-LINK摄像头支持IP地址自动跟随啦!
  5. AE的QuickTime问题
  6. java html转pdf 无法支持中文_java转pdf(html转为pdf),解决中文乱码,标签不规范等问题...
  7. 超人游戏_将障碍画在背景中用pygame.mask.from_threshold实现超人和不同颜色障碍精准碰撞检测
  8. 新手怎么通过网络推广引流
  9. 3Dmax与BIM模型的区别
  10. android wifi认证,android 怎么检测连接的wlan wifi需要portal认证