题意很简单,把文章中的引用信息整理到最后并给它们重新编号。。我想不懂为什么我自己写hash效率那么低。。。还有就是看清题目,空行是仅仅包括空格、TAB、回车的行
1 # include <stdio.h>
2 # include <stdlib.h>
3 # include <string.h>
4 //char tmp[100];
5 struct part
6 {
7 char tmp[3][100];
8 int c;
9 int id;
10 }refer[40001];
11 int c=0,hashc=1;
12 int hash_map[100001][2];
13 void puthash(int num,int val)
14 {
15 int h=num%99997;
16 while(hash_map[h][0]!=-1&&hash_map[h][0]!=num)
17 h=(h+1)%100000;
18 if(hash_map[h][0]==-1)
19 {
20 hash_map[h][0]=num;
21 hash_map[h][1]=val;
22
23 }
24 }
25 int gethash(int num)
26 {
27 int h=num%99997;
28 while(hash_map[h][0]!=-1&&hash_map[h][0]!=num)
29 h=(h+1)%100000;
30 if(hash_map[h][0]==-1)
31 return -1;
32 else
33 return hash_map[h][1];
34 }
35 int cmp(const void *a,const void *b)
36 {
37 struct part *aa=(struct part *)a;
38 struct part *bb=(struct part *)b;
39 return aa->id-bb->id;
40 }
41 int emptyline(char *str)
42 {
43 int i;
44 if(strlen(str)==0) return 1;
45 for(i=0;i<strlen(str);i++)
46 if(str[i]!=' '&&str[i]!='\t')
47 return 0;
48 return 1;
49 }
50 int main()
51 {
52 // freopen("input.txt","r",stdin);
53 // freopen("ans.txt","w",stdout);
54 int i,j;
55 char str[100];
56 int jud=0;
57 memset(hash_map,-1,sizeof(hash_map));
58
59 while(gets(str))
60 {
61 //if(!strcmp(str,"---")) break;
62 if(str[0]=='[')
63 {
64 for(i=0;str[i]!=']';i++);
65 str[i]='\0';
66 refer[c].id=atoi(str+1);
67 refer[c].c=0;
68 strcpy(refer[c].tmp[refer[c].c++],str+i+1);
69 while(gets(str))
70 {
71 if(emptyline(str)) break;
72
73 else
74 {
75 strcpy(refer[c].tmp[refer[c].c++],str);
76 }
77 }
78 c++;
79 }
80 else if(emptyline(str)) continue;
81 else
82 {
83 if(jud==0)
84 jud=1;
85 else
86 printf("\n");
87 do
88 {
89 if(emptyline(str)) break;
90
91 for(i=0;i<strlen(str);i++)
92 {
93 if(str[i]!='[')
94 printf("%c",str[i]);
95 else
96 {
97 int num,h;
98 for(j=i;str[j]!=']';j++);
99 str[j]='\0';
100 num=atoi(str+i+1);
101 str[j]=']';
102 h=gethash(num);
103 if(h==-1)
104 {
105 puthash(num,hashc);
106 h=hashc++;
107 }
108 printf("[%d]",h);
109 i=j;
110 }
111 }
112 printf("\n");
113
114 }while(gets(str));
115 }
116 }
117 for(i=0;i<c;i++)
118 {
119 int res=gethash(refer[i].id);
120 if(res==-1)
121 {
122 puthash(refer[i].id,hashc);
123 res=hashc++;
124 }
125 refer[i].id=res;
126 }
127 qsort(refer,c,sizeof(struct part),cmp);
128
129 for(i=0;i<c;i++)
130 {
131 printf("\n");
132 printf("[%d]",refer[i].id);
133 for(j=0;j<refer[i].c;j++)
134 printf("%s\n",refer[i].tmp[j]);
135 }
136 printf("\n");
137 return 0;
138 }
139