B : Heshen's Account Book
时间限制:1000ms 单点时限:1000ms 内存限制:512MB
描述
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.
输入
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.
输出
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.
样例解释
We assume there is no spaces at the end of each line in the Sample Input.
In the sample input, the '3' at the end of the second line and the '2' at the beginning of the third line are considered consecutive, so "1323" is a number. But this number only belongs to the second line, so the third line has only 2 numbers ---- 14 and 344..
样例输入
a19 01 17b 12 bdc 13 23 14 344 bc
样例输出
12 1323 14 344 0 2 2
题意:
给你一个文本,文本中只包含小写字母,空格和数字字符,然后你要提炼出文本中所有的合法数字,且还要输出每一行有多少个合法数字,其中合法数字满足:
- 一串连续的数字字符
- 前面和后面都不能紧接小写字母
- 不能出现前导0
除此之外,如果某一行的末尾是数字字符,下一行的开头也是数字字符,那么它们可以被视作连在一起的,不过这段数字若合法仍然算在前一行里面
思路:
看起来就是个签到题,但是坑比较多,比如单个0,或者被段切割的数字等等
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL unsigned long long
#define mod 1000000007
char str[200005];
int ans[1005], id[200005];
LL p[200005];
int main(void)
{
LL now;
int n, i, ok, cnt, bel, tot, len;
len = 1, cnt = tot = 0;
bel = ok = now = 0;
while(gets(str+len)!=NULL)
{
cnt++;
n = len-1+strlen(str+len);
if(len!=1 && (str[len]<'0' || str[len]>'9' || str[len-1]<'0' || str[len-1]>'9'))
{
for(i=n;i>=len;i--)
str[i+1] = str[i];
str[len] = ' ';
n++;
}
for(i=len;i<=n;i++)
id[i] = cnt;
len = n+1;
}
str[++n] = ' ';
for(i=1;i<=n;i++)
{
if(str[i]>='1' && str[i]<='9')
{
if(ok==1)
now = now*10+str[i]-'0';
else
{
if(str[i-1]==' ' || i==1)
{
ok = 1, bel = id[i];
now = str[i]-'0';
}
}
}
else if(str[i]=='0')
{
if(ok==1)
now = now*10;
else
{
if((str[i-1]==' ' || i==1) && str[i+1]==' ')
{
ans[++tot] = 0;
ans[id[i]]++;
}
}
}
else if(str[i]==' ')
{
if(ok==1)
{
p[++tot] = now;
ans[bel]++;
now = ok = 0;
}
}
else
{
if(ok==1)
ok = 0, now = 0;
}
}
if(tot>=1)
{
printf("%llu", p[1]);
for(i=2;i<=tot;i++)
printf(" %llu", p[i]);
}
printf("\n");
for(i=1;i<=cnt;i++)
printf("%d\n", ans[i]);
return 0;
}