• Colorful Lecture Note


    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


    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]);
            
            
            
        }
    }
  • 相关阅读:
    [剑指offer] 42. 和为S的两个数字
    [剑指offer] 41. 和为S的连续正数序列
    datagridview bindingsource刷新数据
    datagridview bindingsource
    slot-具名插槽
    基于uniapp的ui库
    组件之间通讯
    vue实现word或pdf文档导出的功能
    Vue之富文本tinymce爬坑录
    12道vue高频原理面试题,你能答出几道?
  • 原文地址:https://www.cnblogs.com/udld/p/5022706.html
Copyright © 2020-2023  润新知