您的位置:首页 > 大数据 > 人工智能

hdu1022 Train Problem I

2016-02-01 17:04 435 查看
体验了下自己写栈和用STL栈的区别,不过我觉得两种方法都要会,毕竟一个原理~

自己写栈:

#include <stdio.h>
#include <string.h>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int N = 1009;
int k;

char stackk
;

void push(char x)
{
stackk[k ++] = x;
}

char pop()
{
k --;
return stackk[k];
}

int sempty()
{
if(k == 0) return 1;
else return 0;
}

int main()
{
// freopen("in.txt", "r", stdin);
int p, n, i, j;
int vis
;
char in
, out
;
while(~scanf("%d", &n))
{
p = i = j = k = 0;
cin >> in >> out;
memset(stackk, 0, sizeof(stackk));
memset(vis, -1, sizeof(vis));
while(j < n)
{
if((sempty() == 1) || (stackk[k - 1] != out[j] && i < n))
{
push(in[i]);
vis[p ++] = 1;
i ++;
}
else
{
if(stackk[k - 1] == out[j])
{
pop();
vis[p ++] = 0;
j ++;
}
else break;
}
}
if(sempty())
{
printf("Yes.\n");
for(int h = 0; h < p; h ++)
{
if(vis[h]) printf("in\n");
else printf("out\n");
}
printf("FINISH\n");
}
else printf("No.\nFINISH\n");
}
return 0;
}


用STL栈:

#include <stdio.h>
#include <string.h>
#include <cstdio>
#include <cstring>
#include <stack>
#include <iostream>

using namespace std;

const int N = 1009;

int main()
{
// freopen("in.txt", "r", stdin);
int p, n, i, j;
int vis
;
char in
, out
;
while(~scanf("%d", &n))
{
stack <char> a;
p = i = j = 0;
cin >> in >> out;
memset(vis, -1, sizeof(vis));
while(j < n)
{
if(a.empty() || (a.top() != out[j] && i < n))
{
a.push(in[i]);
vis[p ++] = 1;
i ++;
}
else
{
if(a.top() == out[j])
{
a.pop();
vis[p ++] = 0;
j ++;
}
else break;
}
}
if(a.empty())
{
printf("Yes.\n");
for(int h = 0; h < p; h ++)
{
if(vis[h]) printf("in\n");
else printf("out\n");
}
printf("FINISH\n");
}
else printf("No.\nFINISH\n");
}
return 0;
}
今天开始写文章的样式改变下喽,毕竟人是要成长的~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu