Colorful Lecture Note
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
package hiho_75; import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack; public class Main { public static void main(String[] argv){ Scanner in = new Scanner(System.in);
String source = in.nextLine();
in.close();
Stack<Integer> color = new Stack<Integer>();
char[] s = source.toCharArray();
int l = s.length; int[] out = new int[3];
ArrayList<Integer> temp = new ArrayList<Integer>();
ArrayList<Integer> sp_temp = new ArrayList<Integer>();
int space_count=0; //....预处理
for(int i=0; i<l; i++){
int t = -i;
char k = s[i];
if(k=='<'){
if(s[i+1]=='/')
temp.add(t);
else
temp.add(i);
continue;
}
if(k=='>'){
temp.add(i); sp_temp.add(space_count); space_count=0;
continue;
} if(k==' ')
space_count++;
} //......栈处理
int L = temp.size();
int space_n=1;
int[] link = new int[L-1];
for(int i=0; i<L-1; i++){
int j = (int) temp.get(i);
int k = (int) temp.get(i+1);
if(i%2==0){ if(j>=0){
int p = k-j;
if(p==7)
link[i]=-1;
else{
if(p==5)
link[i]=-2;
else
link[i]=-3;
}
}
else{
j=-j;
int p = k-j;
if(p==8)
link[i]=-4;
else{
if(p==6)
link[i]=-5;
else
link[i]=-6;
} }
}
else{
if(k>0)
link[i]=k-j-1-sp_temp.get(space_n++);
else
link[i]=-k-j-1-sp_temp.get(space_n++); }
} //...选择颜色
int [] count = new int[3];
for(int i=L-2; i>=0; i--){ if(i%2==0){ switch(link[i]){ case -4:
color.push(0); break;
case -5:
color.push(1); break;
case -6:
color.push(2); break;
case -1:
color.pop(); break;
case -2:
color.pop(); break;
case -3:
color.pop(); break; }
}
else{
if(!color.isEmpty()){
int k =(int) color.pop();
color.push(k);
switch(k){
case 0: out[0]=out[0]+link[i]; break;
case 1: out[1]=out[1]+link[i]; break;
case 2: out[2]=out[2]+link[i]; break;
}
}
}
} //...输出结果
System.out.println(out[2]+" "+out[0]+" "+out[1]); }
}
Colorful Lecture Note的更多相关文章
- Colorful Lecture Note(手工栈)
题目1 : Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorithm ...
- hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)
#1103 : Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorit ...
- HihoCoder - 1103 Colorful Lecture Note
Little Hi is writing an algorithm lecture note for Little Ho. To make the note more comprehensible, ...
- Leetcode: Number of Islands II && Summary of Union Find
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- awesome-nlp
awesome-nlp A curated list of resources dedicated to Natural Language Processing Maintainers - Keon ...
- Computer Science Theory for the Information Age-4: 一些机器学习算法的简介
一些机器学习算法的简介 本节开始,介绍<Computer Science Theory for the Information Age>一书中第六章(这里先暂时跳过第三章),主要涉及学习以 ...
- UFLDL实验报告3:Self-taught
Self-taught 自我学习器实验报告 1.Self-taught 自我学习实验描述 自我学习是无监督特征学习算法,自我学习意味着算法能够从未标注数据中学习,从而使机器学习算法能够获得更大数量的数 ...
- CodeForce 517 Div 2. C Cram Time
http://codeforces.com/contest/1072/problem/C C. Cram Time time limit per test 1 second memory limit ...
- Stanford CS20学习笔记
Lecture Note 2 Tensorboard P3 Data Structures P4 Math Operations P6 Data Types P7 tf native &&am ...
随机推荐
- dev_alloc_skb(len+16) skb_reserve(skb,2) skb_put(skb,len)
/** * dev_alloc_skb - allocate an skbuff for receiving * @length: length to allocate * * ...
- 简单实现JS上传图片预览功能
HTML代码 <div class="upload"> <input type="button" class="btn" ...
- 136.Single Number---异或、位运算
题目链接 题目大意:给出一串数组,里面的数都是两个,只有一个数是一个,把这个只有一个的数找出来.时间复杂度最好是线性的,空间复杂度最好为O(1). 法一:利用map,空间换时间,代码如下(耗时26ms ...
- linux下使用privoxy将socks转为http代理
此博客不在更新,我的博客新地址:www.liuquanhao.com ----------------------------------------------------------------- ...
- FAQ1: 列表索引和切片问题
问题1. 超过列表成员个数的索引访问列表会出现IndexError错误,但是如果用切片去访问就不会报错,而是返回一个空列表.同样元组也是. >>> a=[1,2,3,4] >& ...
- 网络管理 SNMP基础知识
SNMP Agent快速开发 网友:SmileWolf 发布于: 2007.08.02 16:06 (共有条评论) 查看评论 | 我要评论 摘自 http:/ ...
- SPOJ D-query(莫队算法模板)
题目链接:http://www.spoj.com/problems/DQUERY/ 题目大意:给定一个数组,每次询问一个区间内的不同元素的个数 解题思路:直接套莫队的裸题 #include<cs ...
- Effective STL 阅读笔记: Item 3: Make copying cheap and correct for objects in containers
容器 (Containers) 用来存储对象 (Objects), 但是被存储的对象却并非原原本本是你给他的那一个, 而是你指定对象的一个拷贝.而后续对该容器内存储对象的操作,大多也是基于拷贝的. 拷 ...
- 关于IPMI的几个问题
https://blog.csdn.net/lanyang123456/article/details/51712878
- 长安大学第四届ACM-ICPC“迎新杯”程序设计竞赛-重现赛 G - 彩虹岛套娃
题目描述 俄罗斯套娃是俄罗斯特产的木制玩具,一般由多个一样图案的空心木娃娃一个套一个组成,最多可达十多个,通常为圆柱形,底部平坦可以直立.颜色有红色,蓝色,绿色,紫色等.最普通的图案是一个穿着俄罗斯民 ...