Colorful Lecture Note

本文介绍了一个算法问题:如何计算带有特定彩色标签的文本中各颜色标签覆盖的字符数量。该算法通过解析文本中的标签对,并利用栈来跟踪当前有效的颜色标签,最终统计出红色、黄色和蓝色标签分别覆盖了多少个字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]);
        
        
        
    }
}

 

转载于:https://www.cnblogs.com/udld/p/5022706.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值