본문 바로가기
프로그래밍

Stack 동적알고리즘 연습

by 건우아빠유리남편 2009. 10. 12.
반응형

rice
rcei
i o i i o i o o

foo
oof
i i i o o o
i i o i o o

식으로 나오게끔하기

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace 알고리즘연습4
{
    class TestCase : ICloneable
    {
        string stack;
        string input;
        string output;
        string semioutput;
        ArrayList action=new ArrayList();
        public ArrayList Result
        {
            get
            {
                return action;
            }
        }
        public TestCase(string _input,string _output)
        {
            input = _input;
            output = _output;
            stack = string.Empty;
            semioutput = string.Empty;
           
        }
        public TestCase(string _input, string _output,string st,string _semiout,ArrayList _action)
        {
            input = (string)_input.Clone();
            output = (string)_output.Clone();
            stack = (string)st.Clone();
            semioutput = (string)_semiout.Clone();
            foreach (object o in _action)
            {
                action.Add(o);
            }
        }
        public bool Empty()
        {
            return stack.Length == 0;
        }
        public bool Push(char _in)
        {
            try
            {
                stack += _in;
                return true;
            }
            catch{
                return false;
            }
           
        }
        public char Pop()
        {
            char temp;
            try
            {
                temp = stack[stack.Length - 1];
                stack = stack.Substring(0, stack.Length - 1);
                return temp;
            }
            catch {
                return '\0';
            }
           
        }
        public void TestRun()
        {  
            //flag1 = 만약 입력스트림이 남왔으면
                //현재 TestCase를 복재(복재1)
                //복재한 TestCase에 입력할 문자 한개를 push
                //복재 1을 TestRun
            //flag2 = 만약 스택이 비어있지 않다면
                //현재 TestCase를 복재(복재2)
                //복재한 TestCase에서 한개를 Pop해 옮
                //복재 2를 TestRun
            //flag1==false 과 flag2 == false이면
                //현재 Output을 결과에 추가
        }
        public Object Clone()
        {
            return (Object)new TestCase(input,output,stack,semioutput,action);
        }
        internal bool RemainInput()
        {
            return input.Length > 0;
        }
        internal void MyPush()
        {
            stack+=input[0];
            input = input.Substring(1, input.Length - 1);
            action.Add(1);
        }
        internal void MyPop()
        {
            semioutput += Pop();
            action.Add(-1);
        }
        internal bool OK()
        {
            return output == semioutput;
        }
    }
    class App
    {
        ArrayList con=new ArrayList();
        public void Run()
        {
            string str_OString = Console.ReadLine();
           
            string str_CString = Console.ReadLine();
           
           
            Console.WriteLine("[");
            TestCase testcase = new TestCase(str_OString, str_CString);
            TestDoit(testcase);
            Console.WriteLine("]");
            foreach (ArrayList ar in con)
            {
               
                foreach (int i in ar)
                {
                    if (i == 1)
                    {
                        Console.Write("I");
                    }
                    else
                    {
                        Console.Write("O");
                    }
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
        private void TestDoit(TestCase testcase)
        {
            bool flag1=false;
            bool flag2 = false;
            if (flag1 = testcase.RemainInput()) //flag1 = 만약 입력스트림이 남왔으면
            {
                TestCase t1 =(TestCase) testcase.Clone();//현재 TestCase를 복재(복재1)
                t1.MyPush();//복재한 TestCase에 입력할 문자 한개를 push
                TestDoit(t1);//복재 1을 TestRun
            }
            if (flag2 =(!testcase.Empty()))            //flag2 = 만약 스택이 비어있지 않다면
            {
                TestCase t2 = (TestCase)testcase.Clone();//현재 TestCase를 복재(복재2)
                t2.MyPop();//복재한 TestCase에서 한개를 Pop해 옮
                TestDoit(t2);//복재 2를 TestRun
            }
            if ((flag1 == false) && (flag2 == false))//flag1==false 과 flag2 == false이면
            {
                if (testcase.OK())//현재 Output을 결과에 추가
                {
                    con.Add(testcase.Result);
                }
               
            }
           
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
            app.Run();
        }
    }
}
반응형

'프로그래밍' 카테고리의 다른 글

PHP 란?  (0) 2009.12.06
아스키코드표  (0) 2009.11.04
윈도우 소켓 라이브러리 함수  (0) 2009.10.04
동적알고리즘(동적계획법) 예제와 설명  (0) 2009.09.23
Socket 모델들의 용어 정리2  (0) 2009.09.23

댓글