Colorful Lecture Note(手工栈)

题目1 : 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注意:需要用gets来读空格。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);
#define maxn 1100
struct Sta
{
    char word[maxn];
    int cnt = 0;
};
Sta sta[100];
char a[1200];
int main()
{
    gets(a);
    {
        int k = 1;
        int cnt1 = 0;
        int cnt2 = 0;
        int cnt3 = 0;
        int j;
        for(int i = 0; a[i] != ‘\0‘; i++)
        {
            if(a[i] == ‘ ‘)
                continue;
            int k1 = 0;
            int flag = 0;
            if(a[i]==‘<‘)
            {
                k++;
                for(j = i+1; a[j] != ‘>‘; j++)
                {
                    if(a[j]==‘/‘)
                        flag = 1;
                    sta[k].word[k1++] = a[j];
                }

                sta[k].word[k1] = ‘\0‘;
                i=j;

                if(flag)
                {
                    if(strcmp(sta[k-1].word,"yellow")== 0)
                            cnt1 += sta[k-1].cnt;
                    if(strcmp(sta[k-1].word,"blue")== 0)
                            cnt2 += sta[k-1].cnt;
                    if(strcmp(sta[k-1].word,"red")== 0)
                            cnt3 += sta[k-1].cnt;
                    sta[k-1].cnt = sta[k].cnt = 0;
                    k=k-2;
                }
                continue;
            }
            sta[k].cnt++;
        }
           printf("%d %d %d\n", cnt3,cnt1,cnt2);
    }
    return 0;
}
/*<yellow>aaa<blue>bbb  </blue>ccc</yellow>dddd<red>abc</red>*/
 
时间: 2024-10-05 07:26:35

Colorful Lecture Note(手工栈)的相关文章

hihoCoder - 1103 - Colorful Lecture Note (栈~~)

#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 pla

hdu1506---Largest Rectangle in a Histogram(单调栈)

Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of

dfs手写栈模板

在竞赛中如果系统栈很小的话,过深的递归会让栈溢出,这个时候我们就要自己手写栈,将递归转化成手工栈. 方法其实也很简单. 基本思路上,我们就是用栈不断的pop,push.但是何时push,何时pop呢? 在<算法导论>上对深度优先遍历树的讲解中,在深度遍历中,会对每个节点进行染色,白色为没有被访问过:灰色为被访问过,但是该节点的所有子树还没有完成访问:黑色,节点被访问过,而且该节点的所有子树都被完全的访问. 所以,我们就通过颜色标记来进行判断了. 整体的框架如下: memset(vis,0,si

awesome-nlp

awesome-nlp  A curated list of resources dedicated to Natural Language Processing Maintainers - Keon Kim, Martin Park Please read the contribution guidelines before contributing. Please feel free to pull requests, or email Martin Park ([email protect

UFLDL实验报告3:Self-taught

Self-taught 自我学习器实验报告 1.Self-taught 自我学习实验描述 自我学习是无监督特征学习算法,自我学习意味着算法能够从未标注数据中学习,从而使机器学习算法能够获得更大数量的数据,因而更有可能取得更好的性能.在本实验中,我们将按照自我学习的步骤,使用稀疏自编码器和softmax分类器去构造一个手写数字分类器. 实现流程 Step 1 :产生训输入和测试样本集 Step 2 :训练稀疏自编码器 Step 3 :提取特征 Step 4 :训练和测试softMax分类器 Ste

机器学习(1)—— 线性回归

一 线性回归原理 如何实现线性回归? 主要的思想:熟悉目标函数,计算它们的梯度和优化目标参数集. 这些基础工具是后续复杂算法的基础.更多的关于线性回归的细节参考[Lecture Note], 线性回归的目标: 从输入向量值 $x\in\Re^{n}$ 预测目标值$y$. 例如,我们预测一座房子的价格,其中$y$表示房子的价格,$x$中的元素$x_{j}$表示描述房子的特征(像房子的大小和卧室的数目), 假设我们具有很多的房子的数据特征,其中第$i$个房子的特征用$x^{(i)}$表示,对应的价格

Matplotlib Tutorial(译)

Matplotlib Tutorial(译) 翻译自:Matplotlib tutorialNicolas P. Rougier - Euroscipy 2012 toc{: toc} 这个教程基于可以从scipy lecture note得到的 Mike Müller的教程. 源代码可从这里获得.图像在figures文件夹内,所有的脚本位于scripts文件夹. 所有的代码和材料以Creative Commons Attribution 3.0 United States License (C

SDOI2016R1(不是解题报告)

话说洗澡的时候想了一堆要说的,坐到电脑前反而不知所措了-- 序章 听学长说他们都是省选一周前才停的课,然而我们这届--自聪哥韩大他们在省选两周前悄悄跑路后(据说班主任非常支持),信息小组内部一呼百应,颇有陈胜吴广起义的架势,结果是一天之内人走了一大半,原本准备留下月考的xym: Shallwe已经回老校了,xym和Dmoon等人商量以后决定延迟几天再回去,尽量多学点文化课(然而这并没有什么卵用) 然而到了准备走的时候,班主任那边却不放行了,说什么"必须有年级部的确认""停课不

【Codevs1421】【树上DP】【博弈论】秋静叶&amp;秋穰子

东方幻想乡系列模拟赛th04的题. 也不是很难嘛- 题目描述 Description 在幻想乡,秋姐妹是掌管秋天的神明,作为红叶之神的姐姐静叶和作为丰收之神的妹妹穰子.如果把红叶和果实联系在一起,自然会想到烤红薯.烤红薯需要很多的叶子,才能把红薯烤得很香,所以秋姐妹决定比比谁能够收集到最多的红叶.静叶将红叶分成了N堆(编号1..N),并且规定了它们的选取顺序,刚好形成一颗有向树.在游戏过程中,两人从根节点开始,轮流取走红叶,当一个人取走节点i的红叶后,另一个人只能从节点i的儿子节点中选取一个.当