日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Colorful Lecture Note(栈的模拟)

發(fā)布時(shí)間:2025/3/16 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Colorful Lecture Note(栈的模拟) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
時(shí)間限制:10000ms 單點(diǎn)時(shí)限:1000ms 內(nèi)存限制: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
解題思路:棧的模擬#include<iostream> #include<cstdio> #include<cstring> #include<stack> using namespace std;const int maxn = 1000; char str[maxn]; stack<char> s; //stack<string> s;int main() { bool start;string t;int yellow,red,blue;while(gets(str) != NULL){ if(str[0] == 0) break;while(!s.empty()) s.pop(); //清空棧yellow = red = blue = 0;start = false;int len = strlen(str);for(int i = 0; i < len; i++){if(start == false){if(str[i] == '<')start = true;continue;}if(str[i] == '<' || str[i] == ' ') continue;if(str[i-1] == '<'){if(str[i] == 'y'){s.push('Y');i += 6;}else if(str[i] == 'b'){s.push('B');i += 4;}else if(str[i] == 'r'){s.push('R');i += 3;}else //出現(xiàn)</color>的情況,要退棧。。{int cnt = 0; //計(jì)算出彈出元素個(gè)數(shù)while(s.top() >= 'Z'){cnt++, s.pop();}if(s.top() == 'R'){red += cnt;i += 4;}else if(s.top() == 'B'){blue += cnt;i += 5;}else{yellow += cnt;i += 7;}s.pop();}}else if(!s.empty()) //此時(shí)棧內(nèi)非空,說明該元素任屬于某一種顏色。。{s.push(str[i]);}}printf("%d %d %d\n",red,yellow,blue);}return 0; }

總結(jié)

以上是生活随笔為你收集整理的Colorful Lecture Note(栈的模拟)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。