hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)
#1103 : Colorful Lecture Note
時間限制:10000ms 單點時限: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.
樣例輸入題目分析:字符串處理+棧,假設(shè)當前出現(xiàn)了<yellow>就讓代表yellow的字母y進棧,表示當前處于yellow狀態(tài)。
棧頂元素代表是什么,就表示當前字符是什么顏色的。如果遇到</blue>或</yellow>或</red>,就讓代表其顏色
字符從棧頂出棧。
代碼:
#include <iostream> #include <string> #include <stdio.h> #include <string.h> #include <stack>using namespace std;int main() {char s[1010];int i, j, len;int y=0, b=0, r=0;gets(s); //需要用gets讀取,因為原字符串可能會包含空格,第一次用scanf就WA了,2Ylen=strlen(s);stack<char>sta;for(i=0; i<len; ){if(s[i]=='<'){if(s[i+1]=='y'){sta.push('y'); i+=8; //i+8的目的是跳過一些字符,因為已經(jīng)知道是yellow了,//沒必要繼續(xù)往下挨著比對了}else if(s[i+1]=='b'){sta.push('b'); i+=6;}else if(s[i+1]=='r'){sta.push('r'); i+=5;}else if(s[i+1]=='/' ){sta.pop();if(s[i+2]=='y')i+=9;else if(s[i+2]=='b')i+=7;else if(s[i+2]=='r')i+=6;}}else if(isalpha(s[i])){if(!sta.empty())while( isalpha(s[i])&&i<len ){if(sta.top()=='y')y++;else if(sta.top()=='b')b++;elser++;i++;}elsei++;}elsei++;}printf("%d %d %d\n", r, y, b);return 0; }
?
轉(zhuǎn)載于:https://www.cnblogs.com/yspworld/p/4261501.html
總結(jié)
以上是生活随笔為你收集整理的hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++进制转换问题
- 下一篇: [转]linux(centos)搭建SV