Ternary numeric notation is quite popular in Berland. To telegraph the ternary number the Borze alphabet is used. Digit 0 is transmitted as \u00ab.\u00bb, 1 as \u00ab-.\u00bb and 2 as \u00ab–\u00bb. You are to decode the Borze code, i.e. to find out the ternary number given its representation in Borze alphabet.<\/p>\n\n\n\n
<\/div>\n\n\n\n
Input<\/strong><\/p>\n\n\n\nThe first line contains a number in Borze code. The length of the string is between 1 and 200 characters. It’s guaranteed that the given string is a valid Borze code of some ternary number (this number can have leading zeroes).<\/p>\n\n\n\n
<\/div>\n\n\n\n
Output<\/strong><\/p>\n\n\n\nOutput the decoded ternary number. It can have leading zeroes.<\/p>\n\n\n\n
<\/div>\n\n\n\n
Examples<\/strong><\/p>\n\n\n\nInput<\/td> | .-.–<\/td><\/tr> |
Output<\/td> | 012<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/div>\n\n\n\n Input<\/td> | –.<\/td><\/tr> | Output<\/td> | 20<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/div>\n\n\n\n Input<\/td> | -..-.–<\/td><\/tr> | Output<\/td> | 1012<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/div>\n\n\n\n
\n\n\n\n <\/span>Solution<\/strong><\/span><\/h2>\n\n\n\nWe first observe the problem statement: ‘It’s guaranteed that the given string is a valid Borze code’, which means we don’t need to worry about handling invalid inputs. We can use a simple for loop to determine how to convert the Borze code.<\/p>\n\n\n\n <\/div>\n\n\n\n <\/span>C# Solution<\/strong><\/span><\/h3>\n\n\n\nSolution1<\/strong><\/p>\n\n\n\nusing System.Text;\n\nstring str = Console.ReadLine();\nchar[]charArr = str.ToCharArray();\nStringBuilder res = new StringBuilder();\n\nfor(int i=0; i<charArr.Length; i++){\n \n if(charArr[i]=='.'){\n res.Append("0");\n }\n else if(charArr[i+1]=='.'){\n res.Append("1");\n i+=1;\n }\n else{\n res.Append("2");\n i+=1;\n }\n}\n\nConsole.WriteLine(res.ToString());<\/code><\/pre><\/div>\n\n\n\nWe also utilize StringBuilder <\/em><\/strong>to prevent performance degradation caused by repetitive string concatenation.<\/p>\n\n\n\n
|
|
|