杭电 ACM HDU Train Problem I

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 25008    Accepted Submission(s): 9435

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

Hint

Hint

 
For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can‘t let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output "No.".

Author

两种实现:

#include<iostream>
#include<vector>
#include<stack>
using namespace std;
int main()
{
	int n;
	string str1,str2;
	while(cin>>n)
	{
	    cin>>str1>>str2;
	    vector<string>cmd;
		stack<int>ls;
		int cnt[100];
		for(int i=0;i<str2.size();i++)
			cnt[i]=str2[i]-'0';
		int t=0;
		for(int j=0;j<n;j++)
		{
			ls.push(str1[j]-'0');
			cmd.push_back("in");
			while(!ls.empty()&&ls.top()==cnt[t])
			{
				ls.pop();
				t++;
				cmd.push_back("out");
			}
		}
		if(ls.empty())
			{
			    cout<<"Yes."<<endl;
			    for(int i=0;i<cmd.size();i++)
                    cout<<cmd[i]<<endl;
			}
		else
			cout<<"No."<<endl;
			cout<<"FINISH"<<endl;
	}
	return 0;
}
#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;
int main()
{
    int n;
    int cnt[100];
    string str1,str2;
    while(cin>>n)
    {
        vector<string>cmd;
        cin.get();
        cin>>str1>>str2;
        stack<int>dict;
        for(int i=0; i<str2.size(); i++)
        {
            cnt[i]=str2[i]-'0';

        }
        int c=0;
        dict.push(str1[c]-'0');
        cmd.push_back("in");
        int t=0;
        while(1)
        {
            int x=dict.top();
            while(x==cnt[t])
            {
loop:
                cmd.push_back("out");
                dict.pop();

                if(!dict.empty())
                {
                    int x=dict.top();
                    t++;
                    if(x==cnt[t])
                    {
                        goto loop;
                    }
                }
                else
                {
                    t++;
                    break;
                }
            }
            if(c+1==str1.size())
                break;
            dict.push(str1[++c]-'0');
            cmd.push_back("in");
        }
        if(dict.empty())
        {
            cout<<"Yes."<<endl;
            for(int i=0; i<cmd.size(); i++)
                cout<<cmd[i]<<endl;
        }
        else
            cout<<"No."<<endl;
        cout<<"FINISH"<<endl;
    }
    return 0;
}
时间: 2024-10-05 12:34:25

杭电 ACM HDU Train Problem I的相关文章

杭电ACM hdu 2079 选课时间 (模板)

Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别) Input输入数据的第一行是一个数据T,表示有T组数据.每组数据的第一行是两个整数n(1 <= n <= 40),k(1 <= k <= 8).接着有k行,每行有两个整数a(1 <= a <= 8),b(1 <= b <= 10),表示学分为a的课有b门. Output对于每

杭电ACM HDU 1869 六度分离

1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(small world phenomenon)”的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6个人,即只用6个人就可以将他们联系在一起,因此他的理论也被称为 “六度分离”理论(six degrees of separation).虽然米尔格兰姆的理论屡屡应验,一直也有很多社会学家对其兴趣浓厚,但是在30多年的时间里,它从来就没有得到过严谨的证明,只 是一种带有传奇色彩的假说而已. Lele对这个理论相当有兴趣

杭电 ACM HDU 1217 Arbitrage(最短路 floyd算法)

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5272    Accepted Submission(s): 2418 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform

杭电ACM hdu 1398 Square Coins

Problem Description People in Silverland use square coins. Not only they have square shapes but also their values are square numbers. Coins with values of all square numbers up to 289 (=17^2), i.e., 1-credit coins, 4-credit coins, 9-credit coins, ...

杭电ACM HDU 3790 最短路径问题

给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Input 输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p.最后一行是两个数 s,t;起点s,终点.n和m为0时输入结束. (1<n<=1000, 0<m<100000, s != t) Output 输出 一行有两个数, 最短距离及其花费. Sample Inpu

杭电OJ(HDU)-ACM Steps-Chapter Two-《Biker&#39;s Trip Odometer》《Climbing Worm》《hide handkerchief》《Nasty Hac》

1.2.1 Biker's Trip Odometer #include<stdio.h> #include<math.h> const double PI=acos(-1.0); /* 计算题,根据公式做就行,PI*d*r/(12*5280);res1/t*3600; Sample Input 26 1000 5 27.25 873234 3000 26 0 1000 Sample Output Trip #1: 1.29 928.20 Trip #2: 1179.86 1415

杭电OJ(HDU)-ACMSteps-Chapter Two-《An Easy Task》《Buildings》《decimal system》《Vowel Counting》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=2 1.2.5 #include<stdio.h> /* 题意:找闰年. if((i%4==0 && i%100!=0) || i%400==0)count++; 3 2005 25 1855 12 2004 10000 2108 1904 43236 */ int main() { int t,y,n; int i,count=0; whil

杭电OJ(HDU)-ACMSteps-Chapter Three-《FatMouse&amp;#39; Trade》《今年暑假不AC》《排名》《开门人和关门人》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=3 1.3.1 FatMouse' Trade #include <algorithm> /* 题意:价值/代价的比值来排序,买比值大的. Sample Input 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 Sample Output 13.333 31.500 */ #include<stdio.h>

杭电OJ(HDU)-ACMSteps-Chapter Three-《FatMouse&#39; Trade》《今年暑假不AC》《排名》《开门人和关门人》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=3 1.3.1 FatMouse' Trade #include <algorithm> /* 题意:价值/代价的比值来排序,买比值大的. Sample Input 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 Sample Output 13.333 31.500 */ #include<stdio.h>