uva ac
poj 格式不一样你看着改吧
References
poj 格式不一样你看着改吧
References
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4505 | Accepted: 932 |
Description
Editors of an electronic magazine make draft versions of the documents in the form of text files. However, publications should meet some requirements, in particular, concerning the rules of reference use. Unfortunately, lots of the draft articles violate some rules. It is desirable to develop a computer program that will make a publication satisfy all the rules from a draft version.
Let's call a "paragraph" a set of lines in the article going one after another, so that paragraphs are separated by at least one empty line (an "empty line" is a line that containing no characters different from spaces). Any paragraph can contain an arbitrary number of references. A reference is a positive integer not greater than 999 enclosed in square brackets (for example: [23]). There will be no spaces between the brackets and the number. The square brackets are not used in any other context but reference.
There can be two types of paragraph - "regular" and "reference description". Reference description differs from the regular paragraph because it begins with the reference it describes, for example:
[23] It is the description ...
The opening square bracket will be at the first position of the first line of the "reference description" paragraph (i.e. there will be no spaces before it). No reference description paragraph will contain references inside itself.
Each reference will have exactly one corresponding description and each description will have at least one reference to it.
To convert a draft version to a publication you have to use the following rules.
Let's call a "paragraph" a set of lines in the article going one after another, so that paragraphs are separated by at least one empty line (an "empty line" is a line that containing no characters different from spaces). Any paragraph can contain an arbitrary number of references. A reference is a positive integer not greater than 999 enclosed in square brackets (for example: [23]). There will be no spaces between the brackets and the number. The square brackets are not used in any other context but reference.
There can be two types of paragraph - "regular" and "reference description". Reference description differs from the regular paragraph because it begins with the reference it describes, for example:
[23] It is the description ...
The opening square bracket will be at the first position of the first line of the "reference description" paragraph (i.e. there will be no spaces before it). No reference description paragraph will contain references inside itself.
Each reference will have exactly one corresponding description and each description will have at least one reference to it.
To convert a draft version to a publication you have to use the following rules.
- References should be renumbered by the successive integer numbers starting from one in the order of their first appearance in the regular paragraphs of the source draft version of the document.
- Reference descriptions should be placed at the end of the article ordered by their number.
- The order of "regular" paragraphs in the document should be preserved.
- Your program should not make any other changes to the paragraphs.
Input
The input file will be a text file containing a draft article your program should process. All lines will be no more than 80 characters long. Any reference description will contain no more than 3 lines. The input file will contain up to 40000 lines.
Output
The output file contains the result of processing. All paragraphs should be separated by one "true" empty line (i.e. a line that contains no characters at all). There should be no empty lines before the first paragraph.
Sample Input
[5] Brownell, D, "Dynamic Reverse Address Resolution Protocol (DRARP)", Work in Progress. The Reverse Address Resolution Protocol (RARP) [10] (through the extensions defined in the Dynamic RARP (DRARP) [5]) explicitly addresses the problem of network address discovery, and includes an automatic IP address assignment mechanism. [10] Finlayson, R., Mann, T., Mogul, J., and M. Theimer, "A Reverse Address Resolution Protocol", RFC 903, Stanford, June 1984. [16] Postel, J., "Internet Control Message Protocol", STD 5, RFC 792, USC/Information Sciences Institute, September 1981. The Trivial File Transfer Protocol (TFTP) [20] provides for transport of a boot image from a boot server. The Internet Control Message Protocol (ICMP) [16] provides for informing hosts of additional routers via "ICMP redirect" messages. [20] Sollins, K., "The TFTP Protocol (Revision 2)", RFC 783, NIC, June 1981. Works [10], [16] and [20] can be obtained via Internet.
Sample Output
The Reverse Address Resolution Protocol (RARP) [1] (through the extensions defined in the Dynamic RARP (DRARP) [2]) explicitly addresses the problem of network address discovery, and includes an automatic IP address assignment mechanism. The Trivial File Transfer Protocol (TFTP) [3] provides for transport of a boot image from a boot server. The Internet Control Message Protocol (ICMP) [4] provides for informing hosts of additional routers via "ICMP redirect" messages. Works [1], [4] and [3] can be obtained via Internet. [1] Finlayson, R., Mann, T., Mogul, J., and M. Theimer, "A Reverse Address Resolution Protocol", RFC 903, Stanford, June 1984. [2] Brownell, D, "Dynamic Reverse Address Resolution Protocol (DRARP)", Work in Progress. [3] Sollins, K., "The TFTP Protocol (Revision 2)", RFC 783, NIC, June 1981. [4] Postel, J., "Internet Control Message Protocol", STD 5, RFC 792, USC/Information Sciences Institute, September 1981.
Source
Northeastern Europe 1997
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<fstream>
using namespace std;
const int maxref=5000;
const int maxcol=380+5;
struct reference{
string desc;
int oldno,newno;
}p[maxref];
bool cmp(reference a,reference b){
return a.newno<b.newno;
}
int refcnt,refsort;
bool isblank(char s[]){
int k;
while(s[k]!='\0'){
if(!isspace(s[k++]))
return false;
}
return true;
}
inline bool isreference(char s[]){
return s[0]=='[';
}
inline int getrefno(const char s[],int st=1){
int refno;
sscanf(s+st,"%d",&refno);
return refno;
}
int searchref(int oldno){
for(int i=0;i<refcnt;i++)
if(p[i].oldno==oldno)
return i;
return -1;
}
int insertref(string desc,int oldno){
int cur=searchref(oldno);
if(cur<0){
cur=refcnt++;
p[cur].newno=0;
}
p[cur].desc=desc;
p[cur].oldno=oldno;
return cur;
}
int getnewno(int oldno){
int k=searchref(oldno);
if(k<0)
k=insertref("",oldno);
if(!p[k].newno)
p[k].newno=++refsort;
return p[k].newno;
}
void proc(char s[]){
int len=strlen(s);
for(int i=0;i<len;i++){
if(s[i]=='['){
int oldno=getrefno(s,i+1);
int newno=getnewno(oldno);
printf("[%d]",newno);
while(s[++i]!=']');
}else{
putchar(s[i]);
}
}puts("");
}
int main(){
refcnt=0;
refsort=0;
char s[maxcol];
while(gets(s)!=NULL){
while(isblank(s))
if(gets(s)==NULL)
break;
if(s==NULL)
break;
if(isreference(s)) {
int oldno=getrefno(s);
string desc;
do{
desc+=s;
desc+='\n';
}while(gets(s)!=NULL&&!isblank(s));
insertref(desc,oldno);
}else{
do{
proc(s);
}while(gets(s)!=NULL&&!isblank(s));
putchar('\n');
}
}
sort(p,p+refcnt,cmp);
for(int i=0;i<refcnt;i++){
printf("[%d]",p[i].newno);
int k=0;
while(p[i].desc[k++]!=']');
if(i==refcnt-1)
printf("%s",p[i].desc.c_str()+k);
else
printf("%s\n",p[i].desc.c_str()+k);
}
}