#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了,2Ylen=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++;elser++;i++;}elsei++;}elsei++;}printf("%d %d %d\n", r, y, b);return 0;
}

转载于:https://www.cnblogs.com/yspworld/p/4261501.html

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

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

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

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

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

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

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

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

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

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

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

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

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

  7. Colorful Lecture Note(栈的模拟)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorithm lecture note for Little Ho. ...

  8. 微软确认:从4月13日起,Win10系统将强制卸载旧版Edge浏览器

    在经过了一年多的运行测试之后,微软任务自己新开发的基于Chrome的浏览器Edge终于可堪重任,决定在Windows 10系统上全面启用,为了推动新一代浏览器Edge的全面普及,微软决定从2021年4 ...

  9. win7微软停止服务器,微软将于明年1月14日对Windows 7终止支持,那Win7系统还能用吗?...

    "极客谈科技",全新视角.全新思路,伴你遨游神奇的科技世界. 当我们与微软Windows XP系统依依不舍的同时,微软Windows 7系统也要与我们离去.微软官方宣布Window ...

最新文章

  1. 【知识星球】有三AI编程与开源框架正式开通
  2. grep 显示前后几行
  3. 电脑打开html不显示图片,网页不显示图片,详细教您网页不显示图片怎么解决
  4. 违反Apache 2.0许可证再分发被指控,火山引擎回应
  5. 用Emacs编写mybatis
  6. yum 安装的时候出现错误
  7. Docker(二) docker帮助、镜像及容器相关操作命令介绍
  8. executeQuery()方法
  9. sed在行首(行尾)添加字符串;在某行后添加多行字符串-转
  10. windows版mysql5.7.18安装
  11. javafx将数据库内容输出到tableview表格
  12. 【CF1312E】Array Shrinking(dp)
  13. MagicKey - 魔兽世界工具(双开工具,类似按键精灵)
  14. Linux 系统设置静态ip地址
  15. 百度必应谷歌图片自动爬取库icrawler使用体会
  16. html 转 pug 在线,pug - HTML模板引擎安装器
  17. sqlite查询空日期类型_sqlite数据类型(时间 日期 ) timestamp 使用
  18. 如何理解yield的用法
  19. 基于QT搭建的网易云音乐
  20. 2021-01-15 记一次微信支付订单号重复问题

热门文章

  1. oracle11g ora 12545,sqlplus本地登录报错ORA-12545
  2. java中给变量赋值_java中变量赋值的理解
  3. What Is Text Mining?
  4. conda(pip) bad interpreter的解决办法
  5. 2020年, image captioning论文汇总
  6. hdu1532(最大流裸题)
  7. 2017年10月05日普及组 约数
  8. 安卓开发fragment报错_android开发 Fragment嵌套调用常见错误
  9. springcloud分布式事务_Spring Cloud学习资源一网打尽!Awesome Spring Cloud v1.0
  10. python gpu加速 显卡_PyTorch-GPU加速实例