0x18 总结与练习

CH1801 括号画家

维护一个栈,遇到左括号进栈,遇到右括号出栈,且如果匹配ans+=2,否则ans=0

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 using namespace std;
 8 const int INF=0x3f3f3f3f;
 9 const int maxn=100000+10;
10 int x[500];
11 stack<int> s;
12 
13 int main() {
14     //freopen("a.txt", "r", stdin);
15     //freopen("a.out", "w", stdout);
16     char a[maxn];
17     x[')']='(';
18     x[']']='[';
19     x['}']='{';
20     scanf("%s", a+1);
21     int flag=1;
22     int n=strlen(a+1), ans=0, tot=0;
23     for (int i=1; i<=n; ++i) {
24         if (a[i]=='('||a[i]=='['||a[i]=='{') {
25             s.push(a[i]);
26             flag=1;
27         }
28         else if (a[i]==')'||a[i]==']'||a[i]=='}') {
29             if (flag) {
30                 if (!s.empty()&&s.top()==x[a[i]]) { 
31                     tot+=2; ans=max(ans, tot);
32                     //printf("%d %d\n", tot, ans);
33                 }
34                 else {
35                     tot=0;
36                     flag=0;
37                     //printf("%d\n", tot);
38                 }
39             }
40             if (!s.empty()) s.pop();
41         }
42     }
43     printf("%d\n", ans);
44     return 0;
45 }
View Code

CH1802 表达式计算4

先将中缀表达式转换为后缀表达式,方法见这里,然后用栈进行计算就可以了,要注意判断一下负数。我居然写了2.5KB?

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <cctype>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <queue>
  9 #include <stack>
 10 using namespace std;
 11 const int INF=0x3f3f3f3f;
 12 const int maxn=30+5;
 13 char a[maxn];
 14 pair<int,int> b[maxn];
 15 stack<char> s;
 16 stack<pair<int,int> > p;
 17 
 18 int main() {
 19     //freopen("a.txt", "r", stdin);
 20     //freopen("a.out", "w", stdout);
 21     scanf("%s", a+1);
 22     int len=strlen(a+1);
 23     int l=0, r=0, st=1;
 24     for (int i=1; i<=len; ++i) {
 25         if (a[i]=='(') l++;
 26         if (a[i]==')') r++; 
 27     }
 28     if (l>r) st=l-r+1;
 29     if (r>l) len-=r-l;
 30     bool flag=1;
 31     for (int i=st; i<=len; ++i) {
 32         if (a[i]=='-' && s.top()=='(') {    //处理负数 
 33             flag=0;
 34         }
 35         if (isdigit(a[i])) {
 36             int sum=a[i]-'0';
 37             int k=i+1;
 38             while (isdigit(a[k])) {
 39                 sum=sum*10+a[k]-'0'; ++k;
 40             }
 41             i=k-1;
 42             if (!flag) { sum=-sum; flag=1;}
 43             p.push(make_pair(0, sum));
 44         }
 45         if (a[i]=='(') s.push('(');
 46         if (a[i]==')') {
 47             while (s.size() && s.top()!='(') {
 48                 p.push(make_pair(1, s.top())); s.pop(); 
 49             }
 50             if (s.size() && s.top()=='(') s.pop();
 51         }
 52         if (a[i]=='*' || a[i]=='/') {
 53             while (s.size() && (s.top()=='*' || s.top()=='/' || s.top()=='^')) {
 54                 p.push(make_pair(1, s.top())); s.pop();
 55             }
 56             s.push(a[i]);
 57         }
 58         if (flag && (a[i]=='+' || a[i]=='-')) {
 59             while (s.size() && (s.top()=='+' || s.top()=='-' || s.top()=='*' || s.top()=='/' || s.top()=='^')) {
 60                 p.push(make_pair(1, s.top())); s.pop();
 61             }
 62             s.push(a[i]);
 63         }
 64         else if (a[i]=='^') {
 65             while (s.size() && (s.top()=='^')) {
 66                 p.push(make_pair(1, s.top())); s.pop();
 67             }
 68             s.push(a[i]);
 69         }
 70     }
 71     while (s.size()) {
 72         p.push(make_pair(1, s.top())); s.pop();
 73     }
 74     int cnt=0;
 75     while (p.size()) {
 76         if (p.top().first==0) /*printf("%d ", p.top().second);*/b[++cnt]=p.top();
 77         else /*printf("%c ", p.top().second);*/b[++cnt]=p.top();
 78         p.pop();
 79     }
 80     reverse(b+1, b+cnt+1);
 81     /*for (int i=1; i<=cnt; ++i) {
 82         if (b[i].first==0) printf("%d ", b[i].second);
 83         else printf("%c ", b[i].second);
 84     }
 85     puts("");*/
 86     stack<int> ans;
 87     for (int i=1; i<=cnt; ++i) {
 88         if (b[i].first==0) ans.push(b[i].second);
 89         else {
 90             int x1=ans.top(); ans.pop();
 91             int x2=ans.top(); ans.pop();
 92             if (b[i].second=='+') ans.push(x1+x2);
 93             else if (b[i].second=='-') ans.push(x2-x1);
 94             else if (b[i].second=='*') ans.push(x1*x2);
 95             else if (b[i].second=='/') ans.push(x2/x1);
 96             else if (b[i].second=='^') ans.push(pow(x2,x1));
 97         }
 98     }
 99     printf("%d\n", ans.top());
100     return 0;
101 }
View Code

 CH1803 City Game

预处理出每个点最大向上高度,然后看作是二维的Largest Rectangle in a Histogram。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 using namespace std;
10 const int INF=0x3f3f3f3f;
11 const int maxn=1000+10;
12 int n, m;
13 char a[maxn][maxn];
14 struct node {
15     int h, w;
16 };
17 stack<node> s;
18 
19 int main() {
20     freopen("a.txt", "r", stdin);
21     //freopen("a.out", "w", stdout);
22     scanf("%d%d", &n, &m);
23     int ans=0;
24     for (int i=1; i<=n; ++i) {
25         for (int j=1; j<=m; ++j) {
26             char s[5];
27             scanf("%s", s+1);
28             a[i][j]=(s[1]=='F')?1+a[i-1][j]:0;
29         }
30         a[i][m+1]=0;
31         s.push(node{a[i][1], 1});
32         for (int j=2; j<=m+1; ++j) {
33             if (a[i][j]>s.top().h) {
34                 s.push(node{a[i][j], 1});
35             }
36             else {
37                 int width=0;
38                 while (s.size() && s.top().h>a[i][j]) {
39                     width+=s.top().w;
40                     ans=max(ans, width*s.top().h);
41                     printf("%d %d\n", width, ans);
42                     s.pop();
43                 }
44                 s.push(node{a[i][j], width+1});
45             }
46         }
47     }
48     printf("%d\n", 3*ans);
49     return 0;
50 }
View Code

 

转载于:https://www.cnblogs.com/kkkstra/p/11118540.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值