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 ...
随机推荐
- PEB及LDR链
PEB地址的取得在NT内核系统中fs寄存器指向TEB结构,TEB+0x30处指向PEB结构,PEB+0x0c处指向PEB_LDR_DATA结构,PEB_LDR_DATA+0x1c处存放一些指向动态链接 ...
- 17 - 路径操作-shutil模块
目录 1 路径操作 1.1 os.path模块 1.2 pathlib模块 1.2.1 目录操作 1.2.2 文件操作 1.3 os 模块 2 shutil模块 2.1 copy复制 2.2 rm删除 ...
- 工具===激活xmind 8
[下载jar包]: https://stormxing.oss-cn-beijing.aliyuncs.com/files/XMindCrack.jar 方法: 打开xmind 8 安装目录的 ...
- C#:Excel上传服务器后导入数据库
- Windows下Oracle数据库自动备份批处理脚本
expdb命令版本 @echo off REM ########################################################### REM # Windows Se ...
- java中常见异常汇总(根据自己遇到的异常不定时更新)
1.java.lang.ArrayIndexOutOfBoundsException:N(数组索引越界异常.如果访问数组元素时指定的索引值小于0,或者大于等于数组的长度,编译程序不会出现任何错误,但运 ...
- caffe源码整个训练过程
Caffe源码 Blob protected: shared_ptr<SyncedMemory> data_; shared_ptr<SyncedMemory> diff_; ...
- /etc/issue 与 /etc/issue.net 文件,telnet登陆前的显示
在使用文本设备(关于文本设备的例子,见下面的说明)登录Linux系统之前,系统会显示一条被称作“issue”的消息: 若是通过本地文本设备(如本地终端,本地虚拟控制台等)登录,则该信息存放与/etc/ ...
- HDU 3085 Nightmare Ⅱ(双向BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
- (四)Spring 对DAO 的支持
第一节:Spring 对JDBC 的支持 1,配置数据源dbcp: 2,使用JdbcTemplate: 3,JdbcDaoSupport 的使用: 4,NamedParameterJdbcTempla ...