HihoCoder - 1871 - Heshen's Account Book (字符串模拟)

题意:

寻找不能有前导字母和后续字母且不能有前导0的数字,上一行的末尾如果能和下一行连起来,要一起算。

思路:

把所有字符串连接起来一起处理。

坑点:单独的0要算一个数字;如果没有数字,则不需要输出空行。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200+10;
const int N = 2e5 + 10;
string s[maxn];
string tmp;
vector<string> v;
int num[N];
int vis[N];
int tag[N];
int main()
{
    string c;
    ios::sync_with_stdio(0);
    int n = 0;
    memset(vis,0,sizeof vis);
    memset(tag,0,sizeof tag);
    while(getline(cin,c))
    {
        s[n++] = c;
    }
    string str = "";
    int cnt = 0;
    for(int i = 0; i < n; i++)
    {
        str += s[i];
        for(int j = 0;  j < s[i].size(); j++)
        {
            if(j == 0) tag[cnt] = 1;
            num[cnt++] = i;
        }
    }
    for(int i = 0; i < str.size(); i++)
    {

        if(str[i] == '0')
        {
            if( i + 1 < str.size() && isdigit(str[i+1]))
            {
                while(isdigit(str[i]) && i < str.size()) i++;
                i--;
                continue;
            }
            if((i == 0 || str[i - 1] == ' '||tag[i-1])  && (i == str.size() - 1 || str[i+1] == ' '||tag[i + 1]) )
            {
                v.push_back("0");
                vis[num[i]] ++;
                continue;
            }
        }

        if(isdigit(str[i]))
        {
            if(i != 0 && isalpha(str[i-1]) && !tag[i])
            {
                while(isdigit(str[i]) && i < str.size())i++;
                continue;
            }
            int st = i;
            if(i == 0 ||str[i - 1] == ' ' || tag[i])
            {
                string t = "";
                while(isdigit(str[i]) && i < str.size())
                {
                    t+=str[i];
                    i++;
                }
                if( i == str.size() || str[i] == ' '||tag[i])
                {
                    v.push_back(t);
                    vis[num[st]] ++;
                    continue;
                }
            }
        }
    }
    for(int i = 0 ; i <v.size(); i++)
    {
        if(i == 0)
            cout<<v[i];
        else
            cout<<' '<<v[i];
    }
    if(v.size())
        cout<<endl;
    for(int i = 0 ; i < n; i++)
    {
        cout<<vis[i]<<endl;
    }
    return 0;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值