hihocoder1871: Heshen's Account Book
http://hihocoder.com/problemset/problem/1871
描述
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
题意:
有多行字符串,现在要求找出其中数字的个数。
有以下条件的不算成数字:
(1)第一个为小写字母如a123
(2)最后一个为小写字母如415sd
(3)有前补0的如001,023
单独的一个0算是一个数字,10dh123也算数字10123。
把所有字符串连接起来,再把每个字符串开头位置记下来,从头到尾遍历一遍即可。
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 1020
char str[N];
char strs[N*1000];
long long ans[1000*N]; //符合条件的数字
int te[N]; //每行字符串开头位置
int data[N]; //每行有多少数字
int main()
{
int len,lens,s=0,i,j,temp,anslen,start;
long long sum;
while(gets(str)!=NULL)
{
s++;
lens=strlen(strs);
if(s==1||strs[lens-1]>='0'&&strs[lens-1]<='9'&&str[0]>='0'&&str[0]<='9')
{
strcat(strs,str);
te[s]=lens; //记录字符串开头位置
}
else
{
strs[lens]=' ';
strs[lens+1]='\0';
strcat(strs,str);
te[s]=lens+1;
}
}
lens=strlen(strs);
strs[lens]=' ';
strs[lens+1]=' ';
strs[lens+2]='\0';
te[s+1]=99999999;
lens=strlen(strs); //总字符串长度
len=0; //数字长度
anslen=0; //数字数量
temp=0;
start=0; //当前数字头位置
sum=0;
for(i=0;i<lens-1;i++)
{
if(strs[i]==' ')
{
start=i+1;
continue;
}
if(strs[i]>='a'&&strs[i]<='z'&&len==0)
{
for(j=i;j<lens;j++)
{
if(strs[j]==' ')
{
i=j;
start=i+1;
break;
}
}
continue;
if(j==lens)
break;
}
if(strs[i]>='0'&&strs[i]<='9')
{
sum=sum*10+(strs[i]-'0');
len++;
}
if(strs[i+1]==' ')
{
if(strs[i]>='a'&&strs[i]<='z')
{
sum=0;
len=0;
start=i+1;
continue;
}
if((len==1&&sum==0)||((long long)(sum/pow(10,len-1))))
{
ans[anslen]=sum;
anslen++;
for(j=1;j<=s+1;j++)
{
if(te[j]>start)
{
data[j-1]++;
break;
}
}
}
start=i+1;
sum=0;
len=0;
continue;
}
}
for(i=0;i<anslen-1;i++)
printf("%lld ",ans[i]);
if(anslen)
printf("%lld\n",ans[anslen-1]);
for(i=1;i<=s;i++)
printf("%d\n",data[i]);
}