HDU 1022
Description
Input
Output
Sample Input
3 123 321 3 123 312
Sample Output
Yes. in in in out out out FINISH No. FINISH 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.".
Hint
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stack>
#include<algorithm>
#define N 100000
using namespace std;
int main(void)
{
int i,j, num, n;
char s[N], ss[N];
int f[N];
while(scanf("%d", &n) != EOF)
{
stack<char>q;
memset(f, 0, sizeof(f));
scanf("%s %s", s, ss);
i = j = num = 0;
while(j < n)
{
if(q.empty() || q.top() != ss[j] && i < n)
{
q.push(s[i]);
f[num++] = 1;
i++;
}
else
{
if(q.top() == ss[j])
{
q.pop();
f[num++] = 0;
j++;
}
else
break;
}
}
if(q.empty())
{
printf("Yes.\n");
for(i = 0; i < num; i++)
{
if(f[i] == 1)
printf("in\n");
else
printf("out\n");
}
printf("FINISH\n");
}
else
{
printf("No.\n");
printf("FINISH\n");
}
}
return 0;
}