现在的位置: 首页 > 综合 > 正文

hdu 1022

2012年06月30日 ⁄ 综合 ⁄ 共 2969字 ⁄ 字号 评论关闭

原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1022

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11508    Accepted Submission(s): 4194


Problem Description
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one
railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train
B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the
trains can get out in an order O2.
 


Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample
Input.
 


Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of
the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
 


Sample Input
3 123 321 3 123 312
 


Sample Output
Yes. in in in out out out FINISH No. FINISH

这题不难,就是栈的合法输出序列,不是仅仅的反序。我的思路与网上看的大多数人思路不一样,下面贴的是 丙哥的代码:

#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
 
int main ()
{
    int n;
    char in[100];
    char out[100];
    int flag[100];//入栈和出栈的标志数组
    while (scanf("%d%s%s",&n,&in,&out) != EOF)
    {
        stack<char>S;
        int j = 0;
        int i = 0;
        for (i; i <= n;)
        {
            if (S.empty())//第一个判断,栈空入栈。否则进入下面判断
            {
                S.push(in[i]);
                flag[i + j] = 0;
                i++;
            }
            if (!S.empty() && S.top() != out[j])//栈不空且和对应的输出元素不相等继续入栈
            {
                S.push(in[i]);
                flag[i + j] = 0;
                i++;
            }
            if (!S.empty() && S.top() == out[j])//栈顶和对应的输出相等让栈顶出栈
            {
                S.pop();
                flag[i + j] = 1;
                j++;
            }
        }
        if (S.empty())//作为能不能是合法输出的标志,为空就合法否则不合法
        {
            cout << "Yes." << endl;
            for (int k = 0; k < 2*n; k++)
            {
                if (flag[k] != 1)
                {
                    cout << "in" << endl;
                }
                else 
                {
                     cout << "out" << endl;
                }
            }
            cout << "FINISH" << endl;
        }
        else 
        {
             cout << "No." << endl;
             cout << "FINISH" << endl;
        }
    }
    return 0;
}

这个思路比较好,比较简单,时间空间复杂度也比较好。下面是我的代码:

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
using namespace std;

int main()
{
    string s1, s2;
    int m;
    while (scanf("%d", &m) != EOF)
    {
          bool Bool = false;
          cin>>s1>>s2;
          int  i;
          stack<char>ss;
          queue<char>qq; 
          queue<string>mark;       
          for (i =0; i < s1.length(); i++)
          {
              char tmp;
              tmp = s1[i];
              qq.push(tmp);
          }
          for (i = 0; i < s2.length(); i++)
          {
              //if (s2[i] == '1') printf("hello\n");
              if (!ss.empty())
              {
                              if ((s2[i] == ss.top()))
                              {
                                         mark.push("out");
                                         ss.pop();
                                         continue;
                              }
              }
              
                  while(1)
                  {
                          if (qq.empty())
                          {
                                         printf("No.\n");
                                         Bool = true;
                                         break;
                          }
                          char tmp;
                          tmp = qq.front();
                          qq.pop();
                          if (tmp == s2[i])
                          {
                                  mark.push("in");
                                  mark.push("out");
                                  break;
                          }
                          else
                          {
                              ss.push(tmp);
                              mark.push("in");
                          }
                  }
                  if (Bool == true)
                  break;
              
          }
          if(Bool == false)
          {
                  printf("Yes.\n");
                  for(i = 0; i < 2 * m; i++)
                  {
                        cout<<mark.front()<<endl;
                        mark.pop();
                  }
          }
          printf("FINISH\n");
    }
    system ("pause");
    return 0;
}

抱歉!评论已关闭.