首页 > 其他 > 详细

hdu1870最基本的栈

时间:2015-08-13 11:49:53      阅读:375      评论:0      收藏:0      [点我收藏+]

hint:努力提高读题能力吧,尤其在竞赛的时候一份好的心态才能诱发无穷的潜力

   做题一定要耐心,细心,专心。心有旁骛只会徒添烦恼。

关于gets(),可以搜一下跟scanf()的差别,主要是返回值和读取的终止符的差别。gets()遇到换行符截止,scanf()遇到空格截止;gets()在读取错误和遇到EOF时返回null,而scanf()遇到EOF返回-1,正常情况下返回值是正确读入变量的个数。

scanf()详见请戳这里

gets()详见请戳这里

愚人节的礼物

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6579    Accepted Submission(s): 4006


Problem Description
四月一日快到了,Vayko想了个愚人的好办法——送礼物。嘿嘿,不要想的太好,这礼物可没那么简单,Vayko为了愚人,准备了一堆盒子,其中有一个盒子里面装了礼物。盒子里面可以再放零个或者多个盒子。假设放礼物的盒子里不再放其他盒子。

用()表示一个盒子,B表示礼物,Vayko想让你帮她算出愚人指数,即最少需要拆多少个盒子才能拿到礼物。
 

 

Input
本题目包含多组测试,请处理到文件结束。
每组测试包含一个长度不大于1000,只包含‘(‘,‘)‘和‘B‘三种字符的字符串,代表Vayko设计的礼物透视图。
你可以假设,每个透视图画的都是合法的。
 

 

Output
对于每组测试,请在一行里面输出愚人指数。
 

 

Sample Input
((((B)()))()) (B)
 

 

Sample Output
4 1
技术分享
 1 /*
 2 */
 3 #include<stdio.h>
 4 #include<math.h>
 5 #include<string.h>
 6 #include<stdlib.h>
 7 #include<ctype.h>
 8 #include <map>
 9 #include <set>
10 #include <cmath>
11 #include <deque>
12 #include <queue>
13 #include <stack>
14 #include <cstdio>
15 #include <cctype>
16 #include <string>
17 #include <vector>
18 #include <cstdlib>
19 #include <cstring>
20 #include <iostream>
21 #include <algorithm>
22 #define LL long long
23 #define INF 0x7fffffff
24 using namespace std;
25 
26  
27 int main()
28 {
29     int n;
30     //freopen("test.txt","r",stdin);
31     char str[1005];
32     while(gets(str)!=NULL)
33     {
34         stack <char> s;
35     
36         for(int i=0;i<strlen(str);i++)
37         {
38                 
39             if(str[i]!=))
40             s.push(str[i]);
41             else if(!s.empty())
42                     s.pop();
43                     //cout<<"UUU"<<endl;
44             if(!s.empty()&&s.top()==B){
45                 cout<<s.size()-1<<endl;
46                 break;
47             }
48         }
49     }
50     return 0;
51 }
View Code

 

hdu1870最基本的栈

原文:http://www.cnblogs.com/sunshiniing/p/4726444.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!