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 ...
随机推荐
- Android上HDMI介绍(基于高通平台)
本文重点针对HDMI在android上的应用,而比较相关的就是overlay机制.overlay在这里只是简单的介绍,后续会有文章再专门详述. 我没记错的话,高通从7X30开始,平台就可以支持HDMI ...
- module 'pytest' has no attribute 'allure'问题解决
安装allure后执行命令后报错module 'pytest' has no attribute 'allure' C:\Users\Desktop\xin>pytest -s -q --all ...
- Mysql存储之ORM框架SQLAlchemy(一)
上一篇我们说了mysql存储的原生语句方式,因为原生语句每次写都比较的复杂,所以这里我们说一种引用实体类的方式来操作数据库. 什么是ORM ORM技术:Object-Relational Mappin ...
- Spring是什么+控制反转和依赖注入
Spring是一个开源框架,是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架. 原因: (1)通过控制反转(IOC)达到松耦合,IOC也就是把控制权交出去,在使用中直接得到对象 (2)提 ...
- ASP .NET CORE MVC 部署Windows 系统上 IIS具体步骤---.Net Core 部署到 IIS位系统中的步骤
一.IIS 配置 启用 Web 服务器 (IIS) 角色并建立角色服务. 1.Windows Ddesktop 桌面操作系统(win7及更高版本) 导航到“控制面板” > “程序” > “ ...
- 企业级-Mysql双主互备高可用负载均衡架构(基于GTID主从复制模式)(原创)
前言: 原理与思想 这里选用GTID主从复制模式Mysql主从复制模式,是为了更加确保主从复制的正确性.健康性与易配性.这里做的是两服务器A,B各有Mysql实例331 ...
- Python+Selenium 自动化实现实例-数据驱动实例
#coding=utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.implicitly_wait(10) ...
- day1作业二:多级菜单
作业二:多级菜单 1.三级菜单 2.可以次选择进入各子菜单 3.所需新知识点:列表.字典 4.打印b回到上一层 5.打印q退出循环 流程图如下: readme: (1)存储三级菜单的字典;设置 ...
- 如何设计一个基于Node.js和Express的网站架构?
前言 今年七月份,我和几个小伙伴们合伙建立了一个开发团队.业务开展如火如荼的同时,团队宣传就提上了日程,所以迫切需要搭建公司网站出来.确定目标后我们就开始考虑如果构建一个企业网站.先是进行业内调查,看 ...
- PyQt5点击按钮产生新窗体
import sys from PyQt5.QtWidgets import QApplication,QWidget from form1 import Ui_Form1 from form2 im ...