B - Heshen's Account Book HihoCoder - 1871

题目链接:https://hihocoder.com/problemset/problem/1871

思路:满满的细节满满的坑,尤其是 123df123 居然也要算成123123 的时候真是惊呆了,我的做法是将所有字符串连起来,如果上一个最后是数字且下一个第一个是数字就不加空格,否则加一个空格,然后记录每个字符属于第几行的,最后遍历一遍合成的串,每个串check一下,记入答案。

终于的AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 5e5 + 50;
 4 int ans[maxn];
 5 bool check(string &s){
 6     if(s.size() == 1 && s[0] == '0') return true;
 7     if(s[0] >= '1' && s[0] <= '9'){}
 8     else return false;
 9     if(s.back() >= '0' && s.back() <= '9'){}
10     else return false;
11     int len = s.size();
12     string x = "";
13     for(int i = 0;i < len;i++){
14         if(s[i] >= '0'&& s[i] <= '9')
15             x += s[i];
16     }
17     s = x;
18     return true;
19 }
20 int pos[maxn];
21 vector<string> v;
22 int main()
23 {
24     string a;
25     string s;
26     int cnt = 1;
27     bool last = false;
28     while(getline(cin,a))
29     {
30         int n = a.size();
31         if(a[0] >= '0' && a[0] <= '9' && last) s += a;
32         else s += ' ' + a;
33         pos[cnt++] = s.size();
34         if(a[n - 1] >= '0' && a[n - 1] <= '9') last = true;
35         else last = false;
36     }
37     int n = cnt;
38     cnt = 1;
39     for(int i = 0;i < s.size();i++)
40     {
41         while(i >= pos[cnt]) cnt++;
42         string now = "";
43         int j;
44         for(j = i;s[j] != ' ' && j < s.size();j++)
45         {
46             now += s[j];
47         }
48         i = j;
49         if(check(now)) v.push_back(now), ans[cnt]++;
50     }
51     for(int i = 0;i < v.size();i++){
52         cout << v[i];
53         if(i == v.size() - 1) cout << endl;
54         else cout << " ";
55     }
56     for(int i = 1;i < n;i++)
57     {
58         cout << ans[i] << endl;
59     }
60     return 0;
61 }

 

转载于:https://www.cnblogs.com/Carered/p/11553569.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中缀表达式转后缀表达式是一种常见的算法,可以通过栈来实现。具体步骤如下: 1. 初始化两个栈,分别为s1和s2,其中s1用于临时存储运算符,s2用于存储后缀表达式。 2. 从左到右扫描中缀表达式,如果遇到操作数,则直接压入s2栈中。 3. 如果遇到运算符,则比较其与s1栈顶运算符的优先级: (1) 如果s1为空,或栈顶运算符为左括号“(”,则直接将此运算符入栈; (2) 否则,若优先级比栈顶运算符的优先级高或相等,则将运算符压入s1栈中; (3) 否则,将s1栈顶的运算符弹出并压入到s2中,再次转到(1)与s1中新的栈顶运算符相比较。 4. 如果遇到括号,则: (1) 如果是左括号“(”,则直接压入s1栈中; (2) 如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号“(”为止,此时将这一对括号丢弃。 5. 重复步骤2至4,直到表达式的最右边。 6. 将s1中剩余的运算符依次弹出并压入s2中。 7. 依次弹出s2中的元素并输出,即为后缀表达式。 下面是Java代码实现中缀表达式转后缀表达式的方法: ``` public static String infixToSuffix(String infix) { Stack<Character> s1 = new Stack<>(); List<Character> s2 = new ArrayList<>(); for (int i = 0; i < infix.length(); i++) { char c = infix.charAt(i); if (Character.isDigit(c)) { s2.add(c); } else if (c == '(') { s1.push(c); } else if (c == ')') { while (!s1.isEmpty() && s1.peek() != '(') { s2.add(s1.pop()); } s1.pop(); } else { while (!s1.isEmpty() && priority(s1.peek()) >= priority(c)) { s2.add(s1.pop()); } s1.push(c); } } while (!s1.isEmpty()) { s2.add(s1.pop()); } StringBuilder sb = new StringBuilder(); for (char c : s2) { sb.append(c); } return sb.toString(); } private static int priority(char c) { if (c == '*' || c == '/') { return 2; } else if (c == '+' || c == '-') { return 1; } else { return 0; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值