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的上下界就可以了. ...
随机推荐
- Codeforces 889C Maximum Element(DP + 计数)
题目链接 Maximum Element 题意 现在有这一段求序列中最大值的程度片段: (假定序列是一个1-n的排列) int fast_max(int n, int a[]) { int ans ...
- mybatis ----SqlSessionManager
今天我们来看看这个类 有些写法还是很经典的 public class SqlSessionManager implements SqlSessionFactory, SqlSession { priv ...
- shell-异步执行
一.启动后台子任务 在执行命令后加&操作符,表示将命令放在子shell中异步执行.可以达到多线程效果.如下, sleep 10 #等待10秒,再继续下一操作 sleep 10 & #当 ...
- see
Description 问从点(0,0)能看到点(0,0)和(n,n)之间的矩形的多少个整数点,看到(x,y)代表点(0,0)和点(x,y)间没有其他整数点,如看不到(2,4)因为中间有点(1,2) ...
- mybatis学习网站
http://www.mybatis.org/mybatis-3/zh/index.html
- 牛人写的facebook优化php来龙去脉
背景 HHVM 是 Facebook 开发的高性能 PHP 虚拟机,宣称比官方的快9倍,我很好奇,于是抽空简单了解了一下,并整理出这篇文章,希望能回答清楚两方面的问题: HHVM 到底靠谱么?是否可以 ...
- c语言函数---I
函数名: imagesize 功 能: 返回保存位图像所需的字节数 用 法: unsigned far imagesize(int left, int top, int right, int bott ...
- 非常easy的JAVA反射教程
原创文章,转载请注明. 反射能够动态载入类,实例化对象,调用方法.如今以下面样例解说. 一.载入类. Class clazz = Class.forName("java.lang.Strin ...
- JSP技术基础(动态网页基础)
前言:如果说html为静态网页基础,那么jsp就是动态网页基础,两者的区别就是jsp在html的前面多加了几行而已.当然,jsp里面对java的支持度更高.要明白,js只是嵌入在客户端的小程序小脚本而 ...
- GIS+=地理信息+行业+大数据——纽约公开11亿条出租车和Uber原始数据下载及分析
一览众山小编辑团队 原文/ Todd Schneider 翻译/ 沈玮薇 陈翚 文献/ 蒋理 校核/ 众山小编辑/ 众山小 排版/ 徐颖 2014-2015 © 转载请注明:源自公众号"一览 ...