Heshen's Account Book HihoCoder - 1871 2018北京区域赛B题(字符串处理)

Heshen was an official of the Qing dynasty. He made a fortune which could be comparable to a whole country‘s wealth by corruption. So he was known as the most corrupt official in Chinese history. But Emperor Qianlong liked, or even loved him so much that was not punished during Qianlong‘s regime even everybody knew he was totally corrupted.

After Qianlong quit his job, his son Jiaqing took the throne. The new Emperor hated Heshen very much, so he threw Heshen into jail and awarded him death immediately after Qianlong died. Of course Jiaqing would not forget to raid Heshen‘s house for money --- that was the story of "Heshen fell, Jiaqing full."

Jiaqing‘s man got a notebook from Heshen‘s home which was obviously an account book.But the text of the book was written in English! Jiaqing thought all numbers in that account book should stand for money. Please find out all numbers for Jiaqing.

The text of the account book consists only lower case letters, spaces, and digits
(‘0‘ - ‘9‘). One or several consecutive digits form a number. But Jiaqing only cared about the ACTUAL numbers among all numbers. Only if a number DOESN‘T satisfy any of the conditions below, it is an ACTUAL number:

1) The character to the left of the number is a lower case letter, for example: a123

2) The character to the right of the number is a lower case letter, for example: 123b

3) The number has one or more extra leading zeros, such as 01 , 0012….

Please note that if the last character of a line is a digit, and the first character of the next line is also a digit, those two digits are considered consecutive.

Input

There are no more than 200 lines. The length of each line is no more than 1000 characters.

And it is guaranteed that every number‘s length is no more than 18.

There may be spaces at the end of a line, and those spaces matter.

No blank lines in the input. A line consisting of only spaces is not a blank line.

Output

Print all ACTUAL numbers in a single line in the original order.
Then, count the number of ACTUAL numbers of each line, and print them. A number X only belongs to the line which contains the first digit of X.

Sample Input

a19 01 17b
12 bdc 13
23 14 344 bc

Sample Output

12 1323 14 344
0
2
2

题意:给你多行只含有数字,空格,小写字母的字符串。让你从中输出她们想要的数字,并记录每一行有多少个他们想要的数字。他们想要的数字有以下限制:第一个字符不能是字母。最后一个字符不能是字母。不能有前导0.

题目有以下坑点(巨坑,毒瘤):1,123edwe124  是一个有效的数字 它是 123124   (心情:出题人你认真的?) 2:最后一行的尾部没有回车 ,处理的时候要额外注意3:如果一行的最后一个字符是数字,下一行的第一个字符是数字,那么下一行的头部数字是连着上一行的,(奇葩的处理)。4. 只有一个0, 不是前导0,而是一个有效数字0.

解决坑点就按照题意模拟实现就好了,细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘\0‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[400010];
int ans[maxn];
bool check(string str)
{
    if (str == "")
    {
        return 0;
    }
    int len = str.length();
    if (str[0] >= ‘a‘ && str[0] <= ‘z‘)
    {
        return 0;
    } else if (str[len - 1] >= ‘a‘ && str[len - 1] <= ‘z‘)
    {
        return 0;
    } else {
        if (len == 1)
        {
            return 1;
        } else
        {
            if (str[0] == ‘0‘)
            {
                return 0;
            } else
            {
                return 1;
            }
        }
    }
}
int main()
{
    // freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    int n = 0;
    char x;
    while (~scanf("%c", &x))
    {
        s[n++] = x;
    }
    string temp = "";
    int cnt = 1;
    std::vector<string> v;
    int huiche = 0;
    rep(i, 0, n)
    {
        if (s[i] == ‘ ‘)
        {
            if (check(temp))
            {
                // db(temp);
                int num = temp.length();
                string str = "";
                rep(j, 0, num)
                {
                    if (temp[j] >= ‘0‘ && temp[j] <= ‘9‘)
                    {
                        str += temp[j];
                    }
                }
                v.push_back(str);
                ans[cnt]++;
                // cout<<cnt<<" "<<str<<" "<<huiche<<endl;
            }
            cnt += huiche;
            huiche = 0;
            temp = "";
            continue;
        } else if (s[i] == ‘\n‘)
        {
            huiche++;
            int num = temp.length();
            if (s[i - 1] >= ‘0‘ && s[i - 1] <= ‘9‘)
            {
                if (s[i + 1] >= ‘0‘ && s[i + 1] <= ‘9‘)
                {
                    int j = i + 1;
                    while (s[j] != ‘ ‘ && s[j] != ‘\n‘)
                    {
                        temp += s[j++];
                    }
                    i = j - 1;
                } else
                {
                    if (check(temp))
                    {
                        // db(temp);
                        int num = temp.length();
                        string str = "";
                        rep(j, 0, num)
                        {
                            if (temp[j] >= ‘0‘ && temp[j] <= ‘9‘)
                            {
                                str += temp[j];
                            }
                        }
                        v.push_back(str);
                        ans[cnt]++;
                        // cout<<cnt<<" "<<str<<" "<<huiche<<endl;

                    }
                    cnt += huiche;
                    huiche = 0;
                    temp = "";
                    continue;
                }
            } else
            {
                if (check(temp))
                {
                    // db(temp);
                    int num = temp.length();
                    string str = "";
                    rep(j, 0, num)
                    {
                        if (temp[j] >= ‘0‘ && temp[j] <= ‘9‘)
                        {
                            str += temp[j];
                        }
                    }
                    v.push_back(str);
                    ans[cnt]++;
                    // cout<<cnt<<" "<<str<<" "<<huiche<<endl;

                }
                cnt += huiche;
                huiche = 0;
                temp = "";
                continue;
            }
        } else
        {
            temp += s[i];
        }
    }
    if (check(temp))
    {
        // db(temp);
        int num = temp.length();
        string str = "";
        rep(j, 0, num)
        {
            if (temp[j] >= ‘0‘ && temp[j] <= ‘9‘)
            {
                str += temp[j];
            }
        }
        v.push_back(str);
        ans[cnt]++;
        // cout<<cnt<<" "<<str<<" "<<huiche<<endl;
    }
    for (int i = 0; i < sz(v); ++i)
    {
        cout << v[i];
        if (i != sz(v) - 1)
        {
            cout << " ";
        }
    }
    cout << endl;
    repd(i, 1, cnt)
    {
        // cout<<i<<" "<<ans[i]<<endl;
        cout << ans[i] << endl;
    }

    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ‘ ‘ || ch == ‘\n‘);
    if (ch == ‘-‘) {
        *p = -(getchar() - ‘0‘);
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 - ch + ‘0‘;
        }
    }
    else {
        *p = ch - ‘0‘;
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 + ch - ‘0‘;
        }
    }
}

Heshen's Account Book HihoCoder - 1871 2018北京区域赛B题(字符串处理)

原文地址:https://www.cnblogs.com/qieqiemin/p/10806772.html

时间: 2024-11-10 01:04:47

Heshen's Account Book HihoCoder - 1871 2018北京区域赛B题(字符串处理)的相关文章

hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题

题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给定的学生成绩都异或上一次的答案 先将学生按每一门成绩都排一次序 这里将学生分块成sqrt(n)的块数,然后在当前块中用bitset容器来记录含有学生的状态 这里可以记录状态的前缀和,因为比后面成绩好的,必然比前面的学生的成绩也好 查询的时候只要查到正好比他高的学生属于哪一块,这样只要访问sqrt(n

Hihocoder 1634 Puzzle Game(2017 ACM-ICPC 北京区域赛 H题,枚举 + 最大子矩阵变形)

题目链接  2017 Beijing Problem H 题意  给定一个$n * m$的矩阵,现在可以把矩阵中的任意一个数换成$p$,求替换之后最大子矩阵的最小值. 首先想一想暴力的方法,枚举矩阵中的数,然后$O(n^{3})$求最大子矩阵更新答案,这样复杂度是$O(n^{5})$的. 思考得再仔细一些,就是包含这个数的最大子矩阵和,以及不包含这个数的最大子矩阵的和的较大值. 设原矩阵中最大子矩阵和为$mx$. 设$u_{i}$为只考虑矩阵前$i$行的最大子矩阵和,$d_{i}$为考虑矩阵第$

hdu 5112 (2014北京区域赛 A题)

给出某个时刻对应的速度 求出相邻时刻的平均速度 输出最大值 Sample Input23 // n2 2 //t v1 13 430 31 52 0 Sample OutputCase #1: 2.00Case #2: 5.00 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <string>

hdu 5122 (2014北京区域赛 K题)

把一个序列按从小到大排序 要执行多少次操作 只需要从右往左统计,并且不断更新最小值,若当前数为最小值,则将最小值更新为当前数,否则sum+1 Sample Input255 4 3 2 155 1 2 3 4 Sample OutputCase #1: 4Case #2: 1 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5

2018南京区域赛G题 Pyramid——找规律&amp;&amp;递推

先手动推出前10项,再上BM板子求出递推式 $A_n = 5A_{n-1} - 10A_{n-2} + 10A_{n-3} - 5A_{n-4} + A_{n-5}$,根据特征根理论可求出特征方程 $(x-1)^5$,设 $A_n = k_1n^4 + k_2n^3 + k_3n^2+k_4n+k_5$,代入前5项求出系数(用了高斯消元法解方程组). 这样虽然做出来了,但是感觉比较浪费时间,因为BM板子和高斯消元法的板子都不短,对手残狗不友好. 差分 首先前7项分别为1  5 15 35 70

15北京区域赛——A 二分——hihoCoder 1249 Xiongnu&#39;s Land

两次二分,第一次取得最小值,第二次往右二分看是否能到更右边 注意超出部分land部分要去掉 #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; struct edge{ int x, y, w, h; }a[10010]; bool cmp(edge A, edge B) { return A.x < B.x; } int n; ll cal(int x) { ll

HDUOJ-------2493Timer(数学 2008北京现场赛H题)

Timer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 445    Accepted Submission(s): 90 Problem Description Recently, some archaeologists discovered an ancient relic on a small island in the Pa

2015北京网络赛A题The Cats&#39; Feeding Spots

题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<memory.h> 6 using namespace std; 7 const i

HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)

A simple stone game                                                                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)