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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值