hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)
#1103 : Colorful Lecture Note
描述
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日(字符串处理+栈)的更多相关文章
- hihoCoder #1094 : Lost in the City(枚举,微软苏州校招笔试 12月27日 )
#1094 : Lost in the City 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He ...
- 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 ...
- HihoCoder - 1103 Colorful Lecture Note
Little Hi is writing an algorithm lecture note for Little Ho. To make the note more comprehensible, ...
- hihoCoder #1106 : Koch Snowflake 微软苏州校招笔试(1月17日)
描述 Koch Snowflake is one of the most famous factal. It is built by starting with an equilateral tria ...
- Hihocoder #1095 : HIHO Drinking Game (微软苏州校招笔试)( *【二分搜索最优解】)
#1095 : HIHO Drinking Game 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playin ...
- Colorful Lecture Note(手工栈)
题目1 : Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorithm ...
- Colorful Lecture Note
Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorithm lectu ...
- SQL点滴6—“微软不认识闰年2月29日”&字符"N"的作用
原文:SQL点滴6-"微软不认识闰年2月29日"&字符"N"的作用 http://www.cnbeta.com/articles/50580.htm这个 ...
- 【hihocoder】1237 : Farthest Point 微软2016校招在线笔试题
题目:给定一个圆,要你求出一个在里面或者在边上的整数点,使得这个点到原点的距离最大,如果有多个相同,输出x最大,再输出y最大. 思路:对于一个圆,里面整点个数的x是能确定的.你找到x的上下界就可以了. ...
随机推荐
- CentOS 笔记
对安装CentOS安装使用过程中的问题做一个笔记,第一次安装,安装的是7.0版本,最小化安装. 安装环境 :Windows 2012 R2 Standard,Hyper-V Virstual Mach ...
- .Net ToString Format [转]
源文 :http://blog.csdn.net/luyifeiniu/article/category/25663/2 stringstr1 =string.Format("{0:N1}& ...
- js:深入继承
/** * js实现继承: * 1.基于原型链的方式 * 2.基于伪造的方式 * 3.基于组合的方式 */ 一.基于原型链的方式 function Parent(){ this.pv = ...
- 使用纯CSS3实现一个日食动画
日食现象是月亮挡在了地球和太阳之间,也就是月亮遮挡住了太阳. 所以要构造日食,我们须要2个对象:一个代表月亮,一个代表太阳. <div class="eclipse sun" ...
- 别样JAVA学习(五)继承上(1.1)Object类toString()
接下来说完equals以后,我们学习接下来的toString(), Java又觉得全部对象不光具有比較性, 还能使对象变成字符串被打印. 出现 曾经前面显示的是数组.如今显示的是这个对象所属的类. 紧 ...
- 修改Qweb报表
Table of Contents 将报表的类型改为HTML 打印HTML格式报表 使用html editor 修改报表模板 使用Web builder修改报表 恢复报表格式 将报表的 ...
- hihocoder 1032 manachar 求回文串O(n)
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...
- Node.js知识点学习
Node.js知识点学习 一.基本概念 Node.js,或者 Node,是一个可以让 JavaScript 运行在服务器端的平台.可以说,Node.js开创了javascript模块化开发的先河,早期 ...
- iOS之简单瀑布流的实现
iOS之简单瀑布流的实现 前言 超简单的瀑布流实现,这里说一下笔者的思路,详细代码在这里. 实现思路 collectionView能实现各中吊炸天的布局,其精髓就在于UICollectionVie ...
- Linux中的du和df命令
现在也将前阵子学习到du/df两个命令总结一下吧.前阵子测试工作中有遇到过由于磁盘空间满导致程序无法执行到情况,所以使用了df和du两个命令. du查看目录大小,df查看磁盘使用情况.我常使用的命令( ...