2018 ICPC 北京区域赛 B - Heshen‘s Account Book(字符串模拟)

传送门


题目大意

给出若干行字符串,其中每个字符串可能由字母或者数字组成,现在我们需要输出所有的合法数字以及每行合法数字的个数,合法数字是指没有前导0且字符串中不含有字母。然后题目还给出一个条件是如果当前行的结尾字符串和下一行开头字符串都是合法数字,那么他们是被当做拼起来的字符串且归属于上一行。

解题思路

很多细节!首先大体的思路是得到一共有多少行,因为如果下一行只有一个字符串且和上一行能够拼接,因此先将正确的每一行得到,然后去判断每行有多少正确的数字,这里尤其注意当只有一个’0’时是正确的,不能当做前导0,因为这个卡了三个多小时。

#include <bits/stdc++.h>

using namespace std;

vector<string> ans;
vector<int> tot;
vector<int> level;
string s, in;
int line;

bool isNum(string num) {
    if (s.size() == 1 && s[0] == '0') return true;
    if (isalpha(num[0]) || isalpha(num.back()) || num[0] == '0') 
    	return false; 
    for (int i = 1; i < num.size() - 1; i++)
        if (isalpha(num[i])) return false;
    return true;
}

int main() {
    //    ios_base::sync_with_stdio(false);
    //    cin.tie(nullptr), cout.tie(nullptr);
    line = 0;
    while (getline(cin, s) && s[0] != '#') {
        if (!(isdigit(in.back()) && isdigit(s[0])))
            in += " " + s;
        else
            in += s;
        line++;
        level.push_back(in.size() - 1);
    }
    in.push_back(' ');
    tot.resize(line);
    s.clear();
    for (int i = 0; i < in.size(); i++) {
        if (in[i] == ' ') {
            if (s.empty()) continue;
            if (isNum(s)) {
                ans.push_back(s);
                int l = i - s.size();
                for (int j = 0; j < level.size(); j++) {
                    if (l <= level[j]) {
                        tot[j]++;
                        break;
                    }
                }
            }
            s.clear();
            continue;
        }
        s += in[i];
    }
    // cout << in << endl;
    //    for (int i : level) cout << i << endl;
    if (!ans.empty()) {
        bool first = true;
        for (int i = 0; i < ans.size(); i++) {
            if (first) {
                cout << ans[i];
                first = false;
            } else
                cout << " " + ans[i];
        }
        cout << endl;
    }
    for (int it : tot) cout << it << endl;
    return 0;
}

/*
6a
 2
228
a895a
6
6673
6 374
  a7
0
7
#
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值