-
Notifications
You must be signed in to change notification settings - Fork 0
/
6th_lab.c
256 lines (235 loc) · 4.67 KB
/
6th_lab.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
*
* Author: JASKAMAL KAINTH (JK) ( J1K7_7 )
*
*/
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<ctype.h>
#include<string.h>
typedef struct node
{
char *str;
char *dup_str;
int *idx;
int num_idx;
}node;
typedef node* nodeptr;
node words[10000];
int num_words;
int isNumeric(const char *str)
{
while(*str != '\0')
{
if(*str < '0' || *str > '9')
return 0;
str++;
}
return 1;
}
int isword(const char *str)
{
for(;*str ;str++)
{
if(!isalpha(*str))
return 0;
}
return 1;
}
void print_words()
{
int j,i;
for(i = 0; i < num_words; i++)
{
printf("%s ",words[i].str);
for( j = 0; j < words[i].num_idx; j++)
{
if ( j == words[i].num_idx-1 )
printf("%d ",words[i].idx[j]);
else
printf("%d, ",words[i].idx[j]);
}
printf("\n");
}
}
void count_sort(int l)
{
// printf("\nFOR l = %d\n",l);
node out_words[10000];
int count[28];
memset(count,0,sizeof(count));
int i ,j, k;
for( i = 0; i < 10000; i++)
{
out_words[i].str = malloc(100);
out_words[i].dup_str = malloc(100);
out_words[i].idx = malloc(50);
out_words[i].num_idx = 0;
}
// count[0] = '0'
// count[1] = 'a'
for(i = 0; i < num_words; i++)
{
if( words[i].dup_str[l] == '0' )
{
count[0]++;
}
else
{
count[ words[i].dup_str[l] - 'a' + 1]++;
}
}
// for( i = 0; i < 28; i++)
// printf("%d ",count[i]);
// puts("\n");
for(i = 1; i < 28; i++)
count[i] += count[i-1];
// for( i = 0; i < 28; i++)
// printf("%d ",count[i]);
// puts("\nceck");
for(i = num_words-1; i >= 0; i--)
{
if( words[i].dup_str[l] == '0' )
{
out_words[ count[0] - 1] = words[i];
count[0]--;
}
else
{
out_words[ count[ words[i].dup_str[l] - 'a' + 1] - 1] = words[i];
count[words[i].dup_str[l]-'a'+1]--;
}
}
for( i = 0; i < num_words; i++)
words[i] = out_words[i];
// print_words();
}
void update_dup_str(int mx_len)
{
// make every string of same length. add '0' in suffixes
// store in dup_str
int i ,j;
for(i = 0;i < num_words; i++)
{
char str[100] ; strcpy(str,words[i].str);
int lenstr = strlen(str);
int diff = mx_len - lenstr; // add diff '0'
for(j = lenstr; j < mx_len; j++)
{
str[j] = '0';
}
str[j] = '\0';
strcpy(words[i].dup_str,str);
}
}
void radix_sort()
{
int i , l;
int mx_len = 0;
for(i = 0; i < num_words;i++)
{
int temp_len = strlen(words[i].str);
if(temp_len > mx_len )
mx_len = temp_len;
}
update_dup_str(mx_len);
// puts("inside radix_sort");
// print_words();
// printf("%mx_len = %d\n",mx_len);
for(l = mx_len-1; l >= 0; l--)
{
count_sort(l);
}
}
int main()
{
int i = 0 ,j = 0, k = 0 , l = 0;
int curr_page_num = 0;
char line[100];
for( i = 0; i < 10000; i++)
{
words[i].str = malloc(100);
words[i].dup_str = malloc(100);
words[i].idx = malloc(50);
words[i].num_idx = 0;
}
while(1)
{
if( fgets(line,100,stdin) == NULL )
break;
int pagenumberFLAG = 1;
line[strlen(line)-1] = '\0';
for(i = 0; i < strlen(line); i++)
{
if( !isdigit(line[i]) )
pagenumberFLAG = 0;
}
// printf("pagenumberFLAG = %d\n",pagenumberFLAG);
if( pagenumberFLAG == 1)
{
curr_page_num = atoi(line);
continue;
}
// puts(line);
// printf("len of line = %lu\n",strlen(line));
i = 0;
for(; i < strlen(line); )
{
// printf("i* = %d\n",i);
if(line[i] == ' ' )
{
i++;
continue;
}
char temp_str[100];
k = 0;
while(line[i] != ' ')
{
if(line[i] == '\0') break;
// printf("line[%d] = %c\n",i,line[i]);
temp_str[k++] = line[i];
i++;
}
temp_str[k] = '\0';
// printf("i = %d\n",i);
// printf("temp_str = %s\n",temp_str);
// printf("len_temp_str = %lu\n",strlen(temp_str));
// now compare this temp_str with all the existing strings in words[]
int flag_str = 0;
// printf("num_words = %d\n",num_words);
int la = 0;
for(;temp_str[la];la++)temp_str[la] = tolower(temp_str[la]);
for(j = 0;j < num_words; j++)
{
if( strcmp(words[j].str,temp_str) == 0 )
{
flag_str = 1;
if( words[j].idx[words[j].num_idx-1] != curr_page_num )
{
words[j].idx[words[j].num_idx] = curr_page_num;// index updated
words[j].num_idx++;
}
break;
}
}
if(flag_str == 0 )
{
if( isNumeric( temp_str ) ) continue;
if( !isword(temp_str)) continue;
strcpy(words[num_words].str,temp_str); // str updated
words[num_words].idx[0] = curr_page_num;// index updated
words[num_words].num_idx++;
num_words++;
}
}
}
for(i = 0; i < num_words; i++) strcpy(words[i].dup_str,words[i].str);
// puts("PRINTITITITITITITI\n\n\n");
// print_words();
radix_sort();
// puts("\n\nFINAL ANS");
print_words();
/********* input is done *******/
return 0;
}