| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
现在是tleIn Reply To:为什么wa?查不出来 Posted by:testjoan at 2006-03-16 20:02:21 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int C=85;
const int L=40000;
const int R=100;
char pas[L][C];
int LineStatus[L]={0};//0:not judge -1:empty 1:paragraph 2:reference
struct Ref{
int oldno;
int newno;
char text[R*C];
};
Ref Myref[L];
bool isEmpty(char p[C]);
int compare(const void * a,const void * b);
void main(){
char input[C];
int lines;
int lenl;
char temp[4];
int refnotemp;
int indexno;
int refnum;
int i,j,k,l,m,n;
//读入整篇文章
for (i=0;gets(input);i++) strcpy(pas[i],input);
lines=i;
//确定每行的状态(空,注释,正文)
for (i=0;i<lines;i++) if (isEmpty(pas[i])==true) LineStatus[i]=-1;
if (pas[0][0]=='[') LineStatus[0]=2;
else LineStatus[0]=1;
for (i=1;i<lines;i++){
if (LineStatus[i]==0){
if (LineStatus[i-1]==2 || (LineStatus[i-1]==-1 && pas[i][0]=='[')) LineStatus[i]=2;
else LineStatus[i]=1;
}
}
//处理注释段,确定每段注释旧的标号,建立结构关联
for (i=0,m=-1;i<lines;i++){
if (LineStatus[i]==2){
if (pas[i][0]=='['){
m++;
lenl=strlen(pas[i]);
for (j=1,k=0;pas[i][j] != ']';j++,k++) temp[k]=pas[i][j];
temp[k]='\0';
Myref[m].oldno=atoi(temp);//旧标号
for (k=j+1,l=0;k<lenl;k++,l++)
Myref[m].text[l]=pas[i][k];
Myref[m].text[l]='\n';
Myref[m].text[l+1]='\0';//第一行注释
}
else{
strcat(Myref[m].text,pas[i]);//接上后面的注释
strcat(Myref[m].text,"\n");
}
}
}
refnum=m+1;
for (i=0,indexno=1;i<lines;i++){
if (LineStatus[i]==1){
lenl=strlen(pas[i]);
for (j=0;j<lenl;j++){
if (pas[i][j]=='['){
//建立新旧标号的关系
for (k=j+1,l=0;pas[i][k]!=']';k++,l++) temp[l]=pas[i][k];
j=k;
temp[l]='\0';
refnotemp=atoi(temp);
for (k=0;k<refnum;k++){
if(Myref[k].oldno==refnotemp && Myref[k].newno == 0){
Myref[k].newno=indexno;
indexno++;
}
}
//边修改标号边打印正文
for (k=0;k<refnum;k++)
if (Myref[k].oldno==refnotemp) printf("[%d]",Myref[k].newno);
}
else printf("%c",pas[i][j]);
}
printf("\n");
}
else if (LineStatus[i-1] == 1) printf("\n");
}
if (LineStatus[lines-1]==1) printf("\n");
//按照新的标号排序注释,并打印
qsort(Myref,refnum,sizeof(Ref),compare);
for (i=0;i<refnum;i++) printf("[%d]%s\n",Myref[i].newno,Myref[i].text);
}
bool isEmpty(char p[C]){
int ni;
for (ni=0;p[ni]!='\0';ni++)
if (p[ni] != ' '){
return false;
break;
}
return true;
}
int compare(const void * a,const void * b){
Ref *pa,*pb;
pa = (Ref*)a;
pb = (Ref*)b;
return (pa->newno - pb->newno);
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator