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;
}
今天开始写文章的样式改变下喽,毕竟人是要成长的~
自己写栈:
#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 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- 2015-11-11 hdu新生赛 A题(AC)
- 2015-11-11 hdu新生赛 C题(结束后一发AC)
- 2015-11-11 hdu新生赛 E题(结束后一发AC)
- 2015-11-11 hdu新生赛 F题(结束后一发AC)
- hdu-5385