yohei #3512(2007/10/24 08:00 GMT) [ C# ] Rating0/0=0.00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
using System; using System.Text; using System.Collections.Generic; class Program { static string reverseString2(string s) { Stack<string> stack = new Stack<string>(); StringBuilder sb = new StringBuilder(s.Length); string paren = "()[]{}"; foreach (char ch in s) { int index = paren.IndexOf(ch); if (index < 0) { // other sb.Insert(0, ch); } else if (index % 2 == 0) { // ([{ stack.Push(sb.ToString()); stack.Push(ch.ToString()); sb.Remove(0, sb.Length); } else { // )]} if (stack.Count == 0) { sb.Insert(0, ch); } else if (stack.Peek() == paren.Substring(index - 1, 1)) { sb.Insert(0, stack.Pop()).Append(ch); if (stack.Count > 0) sb.Append(stack.Pop()); } else { sb.Insert(0, ch); while (stack.Count > 0) sb.Append(stack.Pop()); } } } while (stack.Count > 0) sb.Append(stack.Pop()); return sb.ToString(); } static void Main(string[] args) { Console.WriteLine(reverseString2("文字列(もじれつ)の反転(はんてん)")); Console.WriteLine(reverseString2("対応[の{とれている(さまざまな)括弧}の(例)]です。")); Console.WriteLine(reverseString2("これ(は(対応のとれていない)括弧がある例です。")); Console.WriteLine(reverseString2("これ(も{対応の)とれていない}括弧の例です")); } }
Rating0/0=0.00-0+
[ reply ]
yohei
#3512()
[
C#
]
Rating0/0=0.00
Rating0/0=0.00-0+
[ reply ]