#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\n", r, y, b);
return 0;
}

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

  1. hihoCoder #1094 : Lost in the City(枚举,微软苏州校招笔试 12月27日 )

    #1094 : Lost in the City 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He ...

  2. hihocoder #1094 : Lost in the City微软苏州校招笔试 12月27日 (建图不大【暴力枚举】 子图的4种形态 1Y )

    #1094 : Lost in the City 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He ...

  3. HihoCoder - 1103 Colorful Lecture Note

    Little Hi is writing an algorithm lecture note for Little Ho. To make the note more comprehensible, ...

  4. hihoCoder #1106 : Koch Snowflake 微软苏州校招笔试(1月17日)

    描述 Koch Snowflake is one of the most famous factal. It is built by starting with an equilateral tria ...

  5. Hihocoder #1095 : HIHO Drinking Game (微软苏州校招笔试)( *【二分搜索最优解】)

    #1095 : HIHO Drinking Game 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playin ...

  6. Colorful Lecture Note(手工栈)

    题目1 : Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorithm ...

  7. Colorful Lecture Note

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

  8. SQL点滴6—“微软不认识闰年2月29日”&字符"N"的作用

    原文:SQL点滴6-"微软不认识闰年2月29日"&字符"N"的作用 http://www.cnbeta.com/articles/50580.htm这个 ...

  9. 【hihocoder】1237 : Farthest Point 微软2016校招在线笔试题

    题目:给定一个圆,要你求出一个在里面或者在边上的整数点,使得这个点到原点的距离最大,如果有多个相同,输出x最大,再输出y最大. 思路:对于一个圆,里面整点个数的x是能确定的.你找到x的上下界就可以了. ...

随机推荐

  1. 洛谷—— P1375 小猫

    https://www.luogu.org/problemnew/show/1375 题目描述 有2n只小猫站成一圈,主人小明想把它们两两之间用绳子绑住尾巴连在一起.同时小明是个完美主义者,不容许看到 ...

  2. 洛谷——P2196 挖地雷

    题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任一处 ...

  3. IntelliJ IDEA ,springboot 2.0 +mybatis 创建和访问数据库

    环境: JDK8+windows10 步骤 New Module —>Spring Initializr—>next 1 ​ 2. ​ 3.web勾选web,sql里面可以不勾,后续添加, ...

  4. Generate C and C++ Header File

    1. 2. 其中bootclasspath 后面的参数就是自己android.jar具体位置 location: ${system_path:javah} working Directoy: ${pr ...

  5. 【JVM】idea启动项目时候 添加jvm启动参数显示详细日志

    -verbose:class

  6. IOS 后台保持连接

    当iphone应用程序进行网络编程时,切到后台后,socket连接会断掉,ios的设计就是这样. 但是好在apple公司也没有那么绝,还是有一些东西可以在后台运行的(backgroundmodes), ...

  7. 百科知识 epub文件如何打开

    .epub 简介 EPub是一个自由的开放标准,属于一种可以"自动重新编排"的内容:也就是文字内容可以根据阅读设备的特性,以最适于阅读的方式显示.EPub档案内部使用了XHTML或 ...

  8. vue2.0 flexible.js + rem 进行自适应开发

    1.在页面中引入flexible.js base.js /** * flexible.js 阿里前端自适应解决方案 */ ;(function(win, lib) { var doc = win.do ...

  9. dos alias/cname address

    diego@localhost sdk/include/Poco/Net]# dig b.wpss.cn ; <<>> DiG - <<>> b.wps ...

  10. MySQL双主热备问题处理

    1. Slave_IO_Running: No mysql> show slave status\G *************************** 1. row *********** ...