Colorful Lecture Note
P2 : Colorful Lecture Note
Description
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
Input contains one line: the text with color tags. The length is no more than 1000 characters.
Output
Output three numbers count_red, count_yellow, count_blue, indicating the numbers of characters colored by red, yellow and blue.
- Sample Input
-
<yellow>aaa<blue>bbb</blue>ccc</yellow>dddd<red>abc</red>
- Sample Output
-
3 6 3
using System; namespace ConsoleApplication2 { class Program { static string[] charColor = new string[3] { "<red>", "<yellow>", "<blue>" }; static string[] charEndColor = new string[3] { "</red>", "</yellow>", "</blue>" }; static int[] countColor = new int[3] { 0, 0, 0 }; static void Main(string[] args) { string line; while ((line = Console.ReadLine()) != null) { //string line = @"<red>aaaa</red><yellow>aaa<blue>bbb</blue>ccc</yellow>dddd<red>abc</red>"; for (int i = 0; i < 3; i++) { CountColor(line, i); } for (int i = 0; i < 3; i++) { Console.Write(countColor[i] + " "); } Console.Write("\n"); } } static void CountColor(string sTargetStr,int nColorIndex) { if (sTargetStr.IndexOf("<") == -1) { return; } string line = sTargetStr; int nSta = line.IndexOf(charColor[nColorIndex]); int nEnd = line.IndexOf(charEndColor[nColorIndex]); if (nSta == -1) return ; if (nEnd == -1) return; string childStr = line.Substring(nSta + charColor[nColorIndex].Length, nEnd - nSta - charEndColor[nColorIndex].Length + 1); string sRemoveStr = charColor[nColorIndex] + childStr + charEndColor[nColorIndex]; int nRemoveSta = line.IndexOf(sRemoveStr); string leftStr = line.Remove(nRemoveSta, sRemoveStr.Length); for (int ix = 0; ix < 3; ix++) { if (ix != nColorIndex) childStr = removeColorPoint(childStr, ix); } countColor[nColorIndex] += childStr.Length; CountColor(leftStr, nColorIndex); } static string removeColorPoint(string sStr, int nRemoveTag) { if (sStr.IndexOf("<") == -1) { return sStr; } int nSta = sStr.IndexOf(charColor[nRemoveTag]); int nEnd = sStr.IndexOf(charEndColor[nRemoveTag]); if(nSta == -1) return sStr; string childStr = sStr.Substring(nSta + charColor[nRemoveTag].Length, nEnd - nSta - charEndColor[nRemoveTag].Length + 1); string sRemoveStr = charColor[nRemoveTag] + childStr + charEndColor[nRemoveTag]; int nRemoveSta = sStr.IndexOf(sRemoveStr); string leftStr = sStr.Remove(nRemoveSta, sRemoveStr.Length); return removeColorPoint(leftStr, nRemoveTag); } } }
由于时间,直接用最笨的方式去实现了。但明显有更好的方式去实现,待实现......