質問編集履歴

3

2番目のコードが100点を取れたコードです。よろしくお願いいたします。

2021/08/22 09:23

投稿

whitehorse85921
whitehorse85921

スコア34

test CHANGED
File without changes
test CHANGED
@@ -96,6 +96,338 @@
96
96
 
97
97
  ```C
98
98
 
99
+
100
+
101
+ #include <stdio.h>
102
+
103
+ #include <string.h>
104
+
105
+ #include <stdlib.h>
106
+
107
+ #include <stddef.h>
108
+
109
+
110
+
111
+ typedef struct {
112
+
113
+ char user[100];
114
+
115
+ char blood[100];
116
+
117
+ char blood2[100];
118
+
119
+ char fortune[100];
120
+
121
+ } User;
122
+
123
+
124
+
125
+ //ハッシュ表
126
+
127
+ typedef struct {
128
+
129
+ User **data;
130
+
131
+ unsigned int size;
132
+
133
+ } HASHTABLE;
134
+
135
+
136
+
137
+ unsigned int MakeHash2(char* str, unsigned int hashmax)
138
+
139
+ {
140
+
141
+ unsigned int n, length, hash, weight;
142
+
143
+
144
+
145
+ length = strlen(str);
146
+
147
+ for (n = weight = hash = 0; n < length; n++, weight++) {
148
+
149
+ if (weight > 7) {
150
+
151
+ weight = 0;
152
+
153
+ }
154
+
155
+ hash += (int)str[n] << (4 * weight);
156
+
157
+ }
158
+
159
+ return hash % hashmax;
160
+
161
+ }
162
+
163
+
164
+
165
+ unsigned int ReHash(HASHTABLE *hashtable, unsigned int firsthash)
166
+
167
+ {
168
+
169
+ unsigned int hashval, k;
170
+
171
+
172
+
173
+ for (k = 1; k <= hashtable->size / 2; k++) {
174
+
175
+ hashval = (firsthash + k * k) % hashtable -> size;
176
+
177
+ if (hashtable->data[hashval] == NULL) {
178
+
179
+ return hashval;
180
+
181
+ }
182
+
183
+ }
184
+
185
+ return -1;
186
+
187
+ }
188
+
189
+
190
+
191
+ void AddDataToMap(HASHTABLE *hashtable, User *newdata)
192
+
193
+ {
194
+
195
+ unsigned int hashval;
196
+
197
+
198
+
199
+ hashval = MakeHash2(newdata->user, hashtable->size);
200
+
201
+
202
+
203
+ if (hashtable->data[hashval] != NULL) {
204
+
205
+ hashval = ReHash(hashtable, hashval);
206
+
207
+ if (hashval == -1) {
208
+
209
+ return;
210
+
211
+ }
212
+
213
+ }
214
+
215
+ hashtable->data[hashval] = newdata;
216
+
217
+ }
218
+
219
+
220
+
221
+ char *GetDataFromMap(HASHTABLE *hashtable, char *key)
222
+
223
+ {
224
+
225
+ unsigned int hashval, k;
226
+
227
+ User *word;
228
+
229
+
230
+
231
+ hashval = MakeHash2(key, hashtable->size);
232
+
233
+
234
+
235
+ for (k = 0; k <= hashtable->size / 2; k++) {
236
+
237
+ word = hashtable->data[(hashval + k * k) % hashtable->size];
238
+
239
+ if (word != NULL) {
240
+
241
+ if (strcmp(key, word->user) == 0) {
242
+
243
+ return word->blood;
244
+
245
+ }
246
+
247
+ }
248
+
249
+ }
250
+
251
+ return NULL;
252
+
253
+ }
254
+
255
+
256
+
257
+ char *GetDataFromMap2(HASHTABLE *hashtable, char *key)
258
+
259
+ {
260
+
261
+ unsigned int hashval, k;
262
+
263
+ User *word;
264
+
265
+
266
+
267
+ hashval = MakeHash2(key, hashtable->size);
268
+
269
+
270
+
271
+ for (k = 0; k <= hashtable->size / 2; k++) {
272
+
273
+ word = hashtable->data[(hashval + k * k) % hashtable->size];
274
+
275
+ if (word != NULL) {
276
+
277
+ if (strcmp(key, word->blood2) == 0) {
278
+
279
+ return word->fortune;
280
+
281
+ }
282
+
283
+ }
284
+
285
+ }
286
+
287
+ return NULL;
288
+
289
+ }
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+ void IniHashTable(HASHTABLE *hashtable, unsigned int size)
298
+
299
+ {
300
+
301
+ hashtable->data = (User**)malloc(sizeof(User*) * size);
302
+
303
+
304
+
305
+ memset(hashtable->data, NULL, sizeof(User*) * size);
306
+
307
+ hashtable->size = size;
308
+
309
+ }
310
+
311
+
312
+
313
+ void CleanupHashTable(HASHTABLE *hashtable)
314
+
315
+ {
316
+
317
+ free(hashtable->data);
318
+
319
+ hashtable->size = 0;
320
+
321
+ }
322
+
323
+
324
+
325
+ void PrintAllData(HASHTABLE *hashtable)
326
+
327
+ {
328
+
329
+ unsigned int n;
330
+
331
+ for (n = 0; n < hashtable->size; n++) {
332
+
333
+ if (hashtable->data[n] != NULL) {
334
+
335
+ printf("%s\n", hashtable->data[n]->fortune);
336
+
337
+ }
338
+
339
+ }
340
+
341
+ }
342
+
343
+
344
+
345
+ int main(void)
346
+
347
+ {
348
+
349
+ char *blood, *blood2;
350
+
351
+ HASHTABLE hashtable;
352
+
353
+
354
+
355
+ IniHashTable(&hashtable, 503);
356
+
357
+
358
+
359
+ char target[100];
360
+
361
+ scanf("%s", target);
362
+
363
+
364
+
365
+ int n;
366
+
367
+ scanf("%d", &n);
368
+
369
+
370
+
371
+ User u[n];
372
+
373
+ for (int i = 0; i < n; i++) {
374
+
375
+ scanf("%s%s", u[i].user, u[i].blood);
376
+
377
+ }
378
+
379
+
380
+
381
+ int m;
382
+
383
+ scanf("%d", &m);
384
+
385
+ for (int i = 0; i < m; i++) {
386
+
387
+ scanf("%s%s", u[i].blood2, u[i].fortune);
388
+
389
+ }
390
+
391
+
392
+
393
+ for (int i = 0; i < n; i++) {
394
+
395
+ AddDataToMap(&hashtable, &u[i]);
396
+
397
+ }
398
+
399
+
400
+
401
+ blood = GetDataFromMap(&hashtable, target);
402
+
403
+
404
+
405
+ blood2 = GetDataFromMap2(&hashtable, blood);
406
+
407
+ if (blood2 != NULL) {
408
+
409
+ printf("%s\n", blood2);
410
+
411
+ }
412
+
413
+
414
+
415
+ CleanupHashTable(&hashtable);
416
+
417
+
418
+
419
+ return 0;
420
+
421
+
422
+
423
+ }
424
+
425
+ ```
426
+
427
+
428
+
429
+ ```C
430
+
99
431
  //エラーはなくなっていませんが100点を取れたコードです。
100
432
 
101
433
  #include <stdio.h>
@@ -114,11 +446,17 @@
114
446
 
115
447
  char blood[100];
116
448
 
449
+ } User;
450
+
451
+
452
+
453
+ typedef struct {
454
+
117
455
  char blood2[100];
118
456
 
119
457
  char fortune[100];
120
458
 
121
- } User;
459
+ } Result;
122
460
 
123
461
 
124
462
 
@@ -132,6 +470,16 @@
132
470
 
133
471
  } HASHTABLE;
134
472
 
473
+
474
+
475
+ typedef struct {
476
+
477
+ Result **data2;
478
+
479
+ unsigned int size2;
480
+
481
+ } HASHTABLE2;
482
+
135
483
 
136
484
 
137
485
  unsigned int MakeHash2(char* str, unsigned int hashmax)
@@ -140,7 +488,7 @@
140
488
 
141
489
  unsigned int n, length, hash, weight;
142
490
 
143
-
491
+
144
492
 
145
493
  length = strlen(str);
146
494
 
@@ -168,7 +516,7 @@
168
516
 
169
517
  unsigned int hashval, k;
170
518
 
171
-
519
+
172
520
 
173
521
  for (k = 1; k <= hashtable->size / 2; k++) {
174
522
 
@@ -188,17 +536,43 @@
188
536
 
189
537
 
190
538
 
539
+ unsigned int ReHash2(HASHTABLE2 *hashtable, unsigned int firsthash)
540
+
541
+ {
542
+
543
+ unsigned int hashval, k;
544
+
545
+
546
+
547
+ for (k = 1; k <= hashtable->size2 / 2; k++) {
548
+
549
+ hashval = (firsthash + k * k) % hashtable -> size2;
550
+
551
+ if (hashtable->data2[hashval] == NULL) {
552
+
553
+ return hashval;
554
+
555
+ }
556
+
557
+ }
558
+
559
+ return -1;
560
+
561
+ }
562
+
563
+
564
+
191
565
  void AddDataToMap(HASHTABLE *hashtable, User *newdata)
192
566
 
193
567
  {
194
568
 
195
569
  unsigned int hashval;
196
570
 
197
-
571
+
198
572
 
199
573
  hashval = MakeHash2(newdata->user, hashtable->size);
200
574
 
201
-
575
+
202
576
 
203
577
  if (hashtable->data[hashval] != NULL) {
204
578
 
@@ -218,6 +592,36 @@
218
592
 
219
593
 
220
594
 
595
+ void AddDataToMap2(HASHTABLE2 *hashtable, Result *newdata)
596
+
597
+ {
598
+
599
+ unsigned int hashval;
600
+
601
+
602
+
603
+ hashval = MakeHash2(newdata->blood2, hashtable->size2);
604
+
605
+
606
+
607
+ if (hashtable->data2[hashval] != NULL) {
608
+
609
+ hashval = ReHash(hashtable, hashval);
610
+
611
+ if (hashval == -1) {
612
+
613
+ return;
614
+
615
+ }
616
+
617
+ }
618
+
619
+ hashtable->data2[hashval] = newdata;
620
+
621
+ }
622
+
623
+
624
+
221
625
  char *GetDataFromMap(HASHTABLE *hashtable, char *key)
222
626
 
223
627
  {
@@ -226,11 +630,11 @@
226
630
 
227
631
  User *word;
228
632
 
229
-
633
+
230
634
 
231
635
  hashval = MakeHash2(key, hashtable->size);
232
636
 
233
-
637
+
234
638
 
235
639
  for (k = 0; k <= hashtable->size / 2; k++) {
236
640
 
@@ -254,23 +658,23 @@
254
658
 
255
659
 
256
660
 
257
- char *GetDataFromMap2(HASHTABLE *hashtable, char *key)
661
+ char *GetDataFromMap2(HASHTABLE2 *hashtable, char *key)
258
662
 
259
663
  {
260
664
 
261
665
  unsigned int hashval, k;
262
666
 
263
- User *word;
667
+ Result *word;
264
-
265
-
266
-
668
+
669
+
670
+
267
- hashval = MakeHash2(key, hashtable->size);
671
+ hashval = MakeHash2(key, hashtable->size2);
268
-
269
-
270
-
672
+
673
+
674
+
271
- for (k = 0; k <= hashtable->size / 2; k++) {
675
+ for (k = 0; k <= hashtable->size2 / 2; k++) {
272
-
676
+
273
- word = hashtable->data[(hashval + k * k) % hashtable->size];
677
+ word = hashtable->data2[(hashval + k * k) % hashtable->size2];
274
678
 
275
679
  if (word != NULL) {
276
680
 
@@ -300,7 +704,7 @@
300
704
 
301
705
  hashtable->data = (User**)malloc(sizeof(User*) * size);
302
706
 
303
-
707
+
304
708
 
305
709
  memset(hashtable->data, NULL, sizeof(User*) * size);
306
710
 
@@ -310,6 +714,22 @@
310
714
 
311
715
 
312
716
 
717
+ void IniHashTable2(HASHTABLE2 *hashtable, unsigned int size)
718
+
719
+ {
720
+
721
+ hashtable->data2 = (User**)malloc(sizeof(User*) * size);
722
+
723
+
724
+
725
+ memset(hashtable->data2, NULL, sizeof(User*) * size);
726
+
727
+ hashtable->size2 = size;
728
+
729
+ }
730
+
731
+
732
+
313
733
  void CleanupHashTable(HASHTABLE *hashtable)
314
734
 
315
735
  {
@@ -322,17 +742,29 @@
322
742
 
323
743
 
324
744
 
745
+ void CleanupHashTable2(HASHTABLE2 *hashtable)
746
+
747
+ {
748
+
749
+ free(hashtable->data2);
750
+
751
+ hashtable->size2 = 0;
752
+
753
+ }
754
+
755
+
756
+
325
- void PrintAllData(HASHTABLE *hashtable)
757
+ void PrintAllData(HASHTABLE2 *hashtable)
326
758
 
327
759
  {
328
760
 
329
761
  unsigned int n;
330
762
 
331
- for (n = 0; n < hashtable->size; n++) {
763
+ for (n = 0; n < hashtable->size2; n++) {
332
-
764
+
333
- if (hashtable->data[n] != NULL) {
765
+ if (hashtable->data2[n] != NULL) {
334
-
766
+
335
- printf("%s\n", hashtable->data[n]->fortune);
767
+ printf("%s\n", hashtable->data2[n]->fortune);
336
768
 
337
769
  }
338
770
 
@@ -350,23 +782,27 @@
350
782
 
351
783
  HASHTABLE hashtable;
352
784
 
353
-
785
+ HASHTABLE2 hashtable2;
786
+
787
+
354
788
 
355
789
  IniHashTable(&hashtable, 503);
356
790
 
357
-
791
+ IniHashTable2(&hashtable2, 503);
792
+
793
+
358
794
 
359
795
  char target[100];
360
796
 
361
797
  scanf("%s", target);
362
798
 
363
-
799
+
364
800
 
365
801
  int n;
366
802
 
367
803
  scanf("%d", &n);
368
804
 
369
-
805
+
370
806
 
371
807
  User u[n];
372
808
 
@@ -376,33 +812,39 @@
376
812
 
377
813
  }
378
814
 
379
-
815
+
380
816
 
381
817
  int m;
382
818
 
383
819
  scanf("%d", &m);
384
820
 
821
+
822
+
823
+ Result r[m];
824
+
385
825
  for (int i = 0; i < m; i++) {
386
826
 
387
- scanf("%s%s", u[i].blood2, u[i].fortune);
827
+ scanf("%s%s", r[i].blood2, r[i].fortune);
388
-
828
+
389
- }
829
+ }
390
-
391
-
830
+
831
+
392
832
 
393
833
  for (int i = 0; i < n; i++) {
394
834
 
395
835
  AddDataToMap(&hashtable, &u[i]);
396
836
 
837
+ AddDataToMap2(&hashtable2, &r[i]);
838
+
397
- }
839
+ }
398
-
399
-
840
+
841
+
400
842
 
401
843
  blood = GetDataFromMap(&hashtable, target);
402
844
 
403
-
404
-
845
+
846
+
405
- blood2 = GetDataFromMap2(&hashtable, blood);
847
+ blood2 = GetDataFromMap2(&hashtable2, blood);
406
848
 
407
849
  if (blood2 != NULL) {
408
850
 
@@ -410,15 +852,17 @@
410
852
 
411
853
  }
412
854
 
413
-
855
+
414
856
 
415
857
  CleanupHashTable(&hashtable);
416
858
 
417
-
859
+ CleanupHashTable2(&hashtable2);
860
+
861
+
418
862
 
419
863
  return 0;
420
864
 
421
-
865
+
422
866
 
423
867
  }
424
868
 
@@ -426,448 +870,6 @@
426
870
 
427
871
 
428
872
 
429
- ```C
430
-
431
- #include <stdio.h>
432
-
433
- #include <string.h>
434
-
435
- #include <stdlib.h>
436
-
437
- #include <stddef.h>
438
-
439
-
440
-
441
- typedef struct {
442
-
443
- char user[100];
444
-
445
- char blood[100];
446
-
447
- } User;
448
-
449
-
450
-
451
- typedef struct {
452
-
453
- char blood2[100];
454
-
455
- char fortune[100];
456
-
457
- } Result;
458
-
459
-
460
-
461
- //ハッシュ表
462
-
463
- typedef struct {
464
-
465
- User **data;
466
-
467
- unsigned int size;
468
-
469
- } HASHTABLE;
470
-
471
-
472
-
473
- typedef struct {
474
-
475
- Result **data2;
476
-
477
- unsigned int size2;
478
-
479
- } HASHTABLE2;
480
-
481
-
482
-
483
- unsigned int MakeHash2(char* str, unsigned int hashmax)
484
-
485
- {
486
-
487
- unsigned int n, length, hash, weight;
488
-
489
-
490
-
491
- length = strlen(str);
492
-
493
- for (n = weight = hash = 0; n < length; n++, weight++) {
494
-
495
- if (weight > 7) {
496
-
497
- weight = 0;
498
-
499
- }
500
-
501
- hash += (int)str[n] << (4 * weight);
502
-
503
- }
504
-
505
- return hash % hashmax;
506
-
507
- }
508
-
509
-
510
-
511
- unsigned int ReHash(HASHTABLE *hashtable, unsigned int firsthash)
512
-
513
- {
514
-
515
- unsigned int hashval, k;
516
-
517
-
518
-
519
- for (k = 1; k <= hashtable->size / 2; k++) {
520
-
521
- hashval = (firsthash + k * k) % hashtable -> size;
522
-
523
- if (hashtable->data[hashval] == NULL) {
524
-
525
- return hashval;
526
-
527
- }
528
-
529
- }
530
-
531
- return -1;
532
-
533
- }
534
-
535
-
536
-
537
- unsigned int ReHash2(HASHTABLE2 *hashtable, unsigned int firsthash)
538
-
539
- {
540
-
541
- unsigned int hashval, k;
542
-
543
-
544
-
545
- for (k = 1; k <= hashtable->size2 / 2; k++) {
546
-
547
- hashval = (firsthash + k * k) % hashtable -> size2;
548
-
549
- if (hashtable->data2[hashval] == NULL) {
550
-
551
- return hashval;
552
-
553
- }
554
-
555
- }
556
-
557
- return -1;
558
-
559
- }
560
-
561
-
562
-
563
- void AddDataToMap(HASHTABLE *hashtable, User *newdata)
564
-
565
- {
566
-
567
- unsigned int hashval;
568
-
569
-
570
-
571
- hashval = MakeHash2(newdata->user, hashtable->size);
572
-
573
-
574
-
575
- if (hashtable->data[hashval] != NULL) {
576
-
577
- hashval = ReHash(hashtable, hashval);
578
-
579
- if (hashval == -1) {
580
-
581
- return;
582
-
583
- }
584
-
585
- }
586
-
587
- hashtable->data[hashval] = newdata;
588
-
589
- }
590
-
591
-
592
-
593
- void AddDataToMap2(HASHTABLE2 *hashtable, Result *newdata)
594
-
595
- {
596
-
597
- unsigned int hashval;
598
-
599
-
600
-
601
- hashval = MakeHash2(newdata->blood2, hashtable->size2);
602
-
603
-
604
-
605
- if (hashtable->data2[hashval] != NULL) {
606
-
607
- hashval = ReHash(hashtable, hashval);
608
-
609
- if (hashval == -1) {
610
-
611
- return;
612
-
613
- }
614
-
615
- }
616
-
617
- hashtable->data2[hashval] = newdata;
618
-
619
- }
620
-
621
-
622
-
623
- char *GetDataFromMap(HASHTABLE *hashtable, char *key)
624
-
625
- {
626
-
627
- unsigned int hashval, k;
628
-
629
- User *word;
630
-
631
-
632
-
633
- hashval = MakeHash2(key, hashtable->size);
634
-
635
-
636
-
637
- for (k = 0; k <= hashtable->size / 2; k++) {
638
-
639
- word = hashtable->data[(hashval + k * k) % hashtable->size];
640
-
641
- if (word != NULL) {
642
-
643
- if (strcmp(key, word->user) == 0) {
644
-
645
- return word->blood;
646
-
647
- }
648
-
649
- }
650
-
651
- }
652
-
653
- return NULL;
654
-
655
- }
656
-
657
-
658
-
659
- char *GetDataFromMap2(HASHTABLE2 *hashtable, char *key)
660
-
661
- {
662
-
663
- unsigned int hashval, k;
664
-
665
- Result *word;
666
-
667
-
668
-
669
- hashval = MakeHash2(key, hashtable->size2);
670
-
671
-
672
-
673
- for (k = 0; k <= hashtable->size2 / 2; k++) {
674
-
675
- word = hashtable->data2[(hashval + k * k) % hashtable->size2];
676
-
677
- if (word != NULL) {
678
-
679
- if (strcmp(key, word->blood2) == 0) {
680
-
681
- return word->fortune;
682
-
683
- }
684
-
685
- }
686
-
687
- }
688
-
689
- return NULL;
690
-
691
- }
692
-
693
-
694
-
695
-
696
-
697
-
698
-
699
- void IniHashTable(HASHTABLE *hashtable, unsigned int size)
700
-
701
- {
702
-
703
- hashtable->data = (User**)malloc(sizeof(User*) * size);
704
-
705
-
706
-
707
- memset(hashtable->data, NULL, sizeof(User*) * size);
708
-
709
- hashtable->size = size;
710
-
711
- }
712
-
713
-
714
-
715
- void IniHashTable2(HASHTABLE2 *hashtable, unsigned int size)
716
-
717
- {
718
-
719
- hashtable->data2 = (User**)malloc(sizeof(User*) * size);
720
-
721
-
722
-
723
- memset(hashtable->data2, NULL, sizeof(User*) * size);
724
-
725
- hashtable->size2 = size;
726
-
727
- }
728
-
729
-
730
-
731
- void CleanupHashTable(HASHTABLE *hashtable)
732
-
733
- {
734
-
735
- free(hashtable->data);
736
-
737
- hashtable->size = 0;
738
-
739
- }
740
-
741
-
742
-
743
- void CleanupHashTable2(HASHTABLE2 *hashtable)
744
-
745
- {
746
-
747
- free(hashtable->data2);
748
-
749
- hashtable->size2 = 0;
750
-
751
- }
752
-
753
-
754
-
755
- void PrintAllData(HASHTABLE2 *hashtable)
756
-
757
- {
758
-
759
- unsigned int n;
760
-
761
- for (n = 0; n < hashtable->size2; n++) {
762
-
763
- if (hashtable->data2[n] != NULL) {
764
-
765
- printf("%s\n", hashtable->data2[n]->fortune);
766
-
767
- }
768
-
769
- }
770
-
771
- }
772
-
773
-
774
-
775
- int main(void)
776
-
777
- {
778
-
779
- char *blood, *blood2;
780
-
781
- HASHTABLE hashtable;
782
-
783
- HASHTABLE2 hashtable2;
784
-
785
-
786
-
787
- IniHashTable(&hashtable, 503);
788
-
789
- IniHashTable2(&hashtable2, 503);
790
-
791
-
792
-
793
- char target[100];
794
-
795
- scanf("%s", target);
796
-
797
-
798
-
799
- int n;
800
-
801
- scanf("%d", &n);
802
-
803
-
804
-
805
- User u[n];
806
-
807
- for (int i = 0; i < n; i++) {
808
-
809
- scanf("%s%s", u[i].user, u[i].blood);
810
-
811
- }
812
-
813
-
814
-
815
- int m;
816
-
817
- scanf("%d", &m);
818
-
819
-
820
-
821
- Result r[m];
822
-
823
- for (int i = 0; i < m; i++) {
824
-
825
- scanf("%s%s", r[i].blood2, r[i].fortune);
826
-
827
- }
828
-
829
-
830
-
831
- for (int i = 0; i < n; i++) {
832
-
833
- AddDataToMap(&hashtable, &u[i]);
834
-
835
- AddDataToMap2(&hashtable2, &r[i]);
836
-
837
- }
838
-
839
-
840
-
841
- blood = GetDataFromMap(&hashtable, target);
842
-
843
-
844
-
845
- blood2 = GetDataFromMap2(&hashtable2, blood);
846
-
847
- if (blood2 != NULL) {
848
-
849
- printf("%s\n", blood2);
850
-
851
- }
852
-
853
-
854
-
855
- CleanupHashTable(&hashtable);
856
-
857
- CleanupHashTable2(&hashtable2);
858
-
859
-
860
-
861
- return 0;
862
-
863
-
864
-
865
- }
866
-
867
- ```
868
-
869
-
870
-
871
873
  ### 試したこと
872
874
 
873
875
 

2

エラーはなくなっていませんが100点を取れたコードを記載しました。

2021/08/22 09:23

投稿

whitehorse85921
whitehorse85921

スコア34

test CHANGED
File without changes
test CHANGED
@@ -96,6 +96,8 @@
96
96
 
97
97
  ```C
98
98
 
99
+ //エラーはなくなっていませんが100点を取れたコードです。
100
+
99
101
  #include <stdio.h>
100
102
 
101
103
  #include <string.h>
@@ -424,6 +426,448 @@
424
426
 
425
427
 
426
428
 
429
+ ```C
430
+
431
+ #include <stdio.h>
432
+
433
+ #include <string.h>
434
+
435
+ #include <stdlib.h>
436
+
437
+ #include <stddef.h>
438
+
439
+
440
+
441
+ typedef struct {
442
+
443
+ char user[100];
444
+
445
+ char blood[100];
446
+
447
+ } User;
448
+
449
+
450
+
451
+ typedef struct {
452
+
453
+ char blood2[100];
454
+
455
+ char fortune[100];
456
+
457
+ } Result;
458
+
459
+
460
+
461
+ //ハッシュ表
462
+
463
+ typedef struct {
464
+
465
+ User **data;
466
+
467
+ unsigned int size;
468
+
469
+ } HASHTABLE;
470
+
471
+
472
+
473
+ typedef struct {
474
+
475
+ Result **data2;
476
+
477
+ unsigned int size2;
478
+
479
+ } HASHTABLE2;
480
+
481
+
482
+
483
+ unsigned int MakeHash2(char* str, unsigned int hashmax)
484
+
485
+ {
486
+
487
+ unsigned int n, length, hash, weight;
488
+
489
+
490
+
491
+ length = strlen(str);
492
+
493
+ for (n = weight = hash = 0; n < length; n++, weight++) {
494
+
495
+ if (weight > 7) {
496
+
497
+ weight = 0;
498
+
499
+ }
500
+
501
+ hash += (int)str[n] << (4 * weight);
502
+
503
+ }
504
+
505
+ return hash % hashmax;
506
+
507
+ }
508
+
509
+
510
+
511
+ unsigned int ReHash(HASHTABLE *hashtable, unsigned int firsthash)
512
+
513
+ {
514
+
515
+ unsigned int hashval, k;
516
+
517
+
518
+
519
+ for (k = 1; k <= hashtable->size / 2; k++) {
520
+
521
+ hashval = (firsthash + k * k) % hashtable -> size;
522
+
523
+ if (hashtable->data[hashval] == NULL) {
524
+
525
+ return hashval;
526
+
527
+ }
528
+
529
+ }
530
+
531
+ return -1;
532
+
533
+ }
534
+
535
+
536
+
537
+ unsigned int ReHash2(HASHTABLE2 *hashtable, unsigned int firsthash)
538
+
539
+ {
540
+
541
+ unsigned int hashval, k;
542
+
543
+
544
+
545
+ for (k = 1; k <= hashtable->size2 / 2; k++) {
546
+
547
+ hashval = (firsthash + k * k) % hashtable -> size2;
548
+
549
+ if (hashtable->data2[hashval] == NULL) {
550
+
551
+ return hashval;
552
+
553
+ }
554
+
555
+ }
556
+
557
+ return -1;
558
+
559
+ }
560
+
561
+
562
+
563
+ void AddDataToMap(HASHTABLE *hashtable, User *newdata)
564
+
565
+ {
566
+
567
+ unsigned int hashval;
568
+
569
+
570
+
571
+ hashval = MakeHash2(newdata->user, hashtable->size);
572
+
573
+
574
+
575
+ if (hashtable->data[hashval] != NULL) {
576
+
577
+ hashval = ReHash(hashtable, hashval);
578
+
579
+ if (hashval == -1) {
580
+
581
+ return;
582
+
583
+ }
584
+
585
+ }
586
+
587
+ hashtable->data[hashval] = newdata;
588
+
589
+ }
590
+
591
+
592
+
593
+ void AddDataToMap2(HASHTABLE2 *hashtable, Result *newdata)
594
+
595
+ {
596
+
597
+ unsigned int hashval;
598
+
599
+
600
+
601
+ hashval = MakeHash2(newdata->blood2, hashtable->size2);
602
+
603
+
604
+
605
+ if (hashtable->data2[hashval] != NULL) {
606
+
607
+ hashval = ReHash(hashtable, hashval);
608
+
609
+ if (hashval == -1) {
610
+
611
+ return;
612
+
613
+ }
614
+
615
+ }
616
+
617
+ hashtable->data2[hashval] = newdata;
618
+
619
+ }
620
+
621
+
622
+
623
+ char *GetDataFromMap(HASHTABLE *hashtable, char *key)
624
+
625
+ {
626
+
627
+ unsigned int hashval, k;
628
+
629
+ User *word;
630
+
631
+
632
+
633
+ hashval = MakeHash2(key, hashtable->size);
634
+
635
+
636
+
637
+ for (k = 0; k <= hashtable->size / 2; k++) {
638
+
639
+ word = hashtable->data[(hashval + k * k) % hashtable->size];
640
+
641
+ if (word != NULL) {
642
+
643
+ if (strcmp(key, word->user) == 0) {
644
+
645
+ return word->blood;
646
+
647
+ }
648
+
649
+ }
650
+
651
+ }
652
+
653
+ return NULL;
654
+
655
+ }
656
+
657
+
658
+
659
+ char *GetDataFromMap2(HASHTABLE2 *hashtable, char *key)
660
+
661
+ {
662
+
663
+ unsigned int hashval, k;
664
+
665
+ Result *word;
666
+
667
+
668
+
669
+ hashval = MakeHash2(key, hashtable->size2);
670
+
671
+
672
+
673
+ for (k = 0; k <= hashtable->size2 / 2; k++) {
674
+
675
+ word = hashtable->data2[(hashval + k * k) % hashtable->size2];
676
+
677
+ if (word != NULL) {
678
+
679
+ if (strcmp(key, word->blood2) == 0) {
680
+
681
+ return word->fortune;
682
+
683
+ }
684
+
685
+ }
686
+
687
+ }
688
+
689
+ return NULL;
690
+
691
+ }
692
+
693
+
694
+
695
+
696
+
697
+
698
+
699
+ void IniHashTable(HASHTABLE *hashtable, unsigned int size)
700
+
701
+ {
702
+
703
+ hashtable->data = (User**)malloc(sizeof(User*) * size);
704
+
705
+
706
+
707
+ memset(hashtable->data, NULL, sizeof(User*) * size);
708
+
709
+ hashtable->size = size;
710
+
711
+ }
712
+
713
+
714
+
715
+ void IniHashTable2(HASHTABLE2 *hashtable, unsigned int size)
716
+
717
+ {
718
+
719
+ hashtable->data2 = (User**)malloc(sizeof(User*) * size);
720
+
721
+
722
+
723
+ memset(hashtable->data2, NULL, sizeof(User*) * size);
724
+
725
+ hashtable->size2 = size;
726
+
727
+ }
728
+
729
+
730
+
731
+ void CleanupHashTable(HASHTABLE *hashtable)
732
+
733
+ {
734
+
735
+ free(hashtable->data);
736
+
737
+ hashtable->size = 0;
738
+
739
+ }
740
+
741
+
742
+
743
+ void CleanupHashTable2(HASHTABLE2 *hashtable)
744
+
745
+ {
746
+
747
+ free(hashtable->data2);
748
+
749
+ hashtable->size2 = 0;
750
+
751
+ }
752
+
753
+
754
+
755
+ void PrintAllData(HASHTABLE2 *hashtable)
756
+
757
+ {
758
+
759
+ unsigned int n;
760
+
761
+ for (n = 0; n < hashtable->size2; n++) {
762
+
763
+ if (hashtable->data2[n] != NULL) {
764
+
765
+ printf("%s\n", hashtable->data2[n]->fortune);
766
+
767
+ }
768
+
769
+ }
770
+
771
+ }
772
+
773
+
774
+
775
+ int main(void)
776
+
777
+ {
778
+
779
+ char *blood, *blood2;
780
+
781
+ HASHTABLE hashtable;
782
+
783
+ HASHTABLE2 hashtable2;
784
+
785
+
786
+
787
+ IniHashTable(&hashtable, 503);
788
+
789
+ IniHashTable2(&hashtable2, 503);
790
+
791
+
792
+
793
+ char target[100];
794
+
795
+ scanf("%s", target);
796
+
797
+
798
+
799
+ int n;
800
+
801
+ scanf("%d", &n);
802
+
803
+
804
+
805
+ User u[n];
806
+
807
+ for (int i = 0; i < n; i++) {
808
+
809
+ scanf("%s%s", u[i].user, u[i].blood);
810
+
811
+ }
812
+
813
+
814
+
815
+ int m;
816
+
817
+ scanf("%d", &m);
818
+
819
+
820
+
821
+ Result r[m];
822
+
823
+ for (int i = 0; i < m; i++) {
824
+
825
+ scanf("%s%s", r[i].blood2, r[i].fortune);
826
+
827
+ }
828
+
829
+
830
+
831
+ for (int i = 0; i < n; i++) {
832
+
833
+ AddDataToMap(&hashtable, &u[i]);
834
+
835
+ AddDataToMap2(&hashtable2, &r[i]);
836
+
837
+ }
838
+
839
+
840
+
841
+ blood = GetDataFromMap(&hashtable, target);
842
+
843
+
844
+
845
+ blood2 = GetDataFromMap2(&hashtable2, blood);
846
+
847
+ if (blood2 != NULL) {
848
+
849
+ printf("%s\n", blood2);
850
+
851
+ }
852
+
853
+
854
+
855
+ CleanupHashTable(&hashtable);
856
+
857
+ CleanupHashTable2(&hashtable2);
858
+
859
+
860
+
861
+ return 0;
862
+
863
+
864
+
865
+ }
866
+
867
+ ```
868
+
869
+
870
+
427
871
  ### 試したこと
428
872
 
429
873
 

1

多すぎた入力データを削りました。

2021/08/22 09:22

投稿

whitehorse85921
whitehorse85921

スコア34

test CHANGED
File without changes
test CHANGED
@@ -56,333 +56,7 @@
56
56
 
57
57
  x rUW1ZgksZXq5pZey
58
58
 
59
- s TNA3A
60
-
61
- DzoWEEN6RlGuu17iKaL EQKjgXYQ9
62
-
63
- nMEQpS 4l3AIz3Wz
64
-
65
- QmKiPEfUQzo RkgYvmps4wlgRxG9
66
-
67
- WCprF 9s7IMWesx
68
-
69
- GF7vqk9rn9Bpb c166l
70
-
71
- NzU48nwY5Zfmdy6D KJGlBRt
72
-
73
- 16fV13c7jd1WkRxC FoH
74
-
75
- 8bBemJ KSE6jTpYBA31MxsiUj
76
-
77
- DsuYTTM7hLsvmkv0Uj WFCd
78
-
79
- jiS8WrXle5Yn34e wXMv
80
-
81
- d a1wJEyBlG9U
82
-
83
- wWWlRa mEiRHWI4sL
84
-
85
- sA3o4XQN7U4Lawyr zK1HgYPyF08Jbks
86
-
87
- wZb XJd39db
88
-
89
- zTE2 Jw0N
90
-
91
- Pi5PX GnQAMeWWQZYTUs1fO8f
92
-
93
- 96pA c166l
94
-
95
- b rJrUe0AB8vILgoqTdl
96
-
97
- VHPpmHjDWPK wXMv
98
-
99
- wHntfo21C00OcjVANYi 3JH4g8l6T6VI
100
-
101
- rqApEtkaMmDUh 5XWcTUY7mSV4
102
-
103
- TEZNhL9krYuW9A KJGlBRt
104
-
105
- ZurB XJd39db
106
-
107
- nAphB7hFowF4x jKU6xZfnxH
108
-
109
- AUwq6C Un
110
-
111
- faMxQZJu0 wXMv
112
-
113
- 6Zx8QcPSclY5FN S9Avu7e4B7e1eHEAgn
114
-
115
- Ul3acUux RkgYvmps4wlgRxG9
116
-
117
- Ygxq3hiJexy m3z3PbBN0FhiQ7lm
118
-
119
- 9iftxHktd5k4L DK7K
120
-
121
- PJ rmcYOrsChsh5H
122
-
123
- leC32JKwZQIVywDmvb jKU6xZfnxH
124
-
125
- Q58l29walHjViPt OZ9UoCoHh0T3
126
-
127
- 5 RfMgENaz4uR
128
-
129
- P0Lyf6I yjK
130
-
131
- 8Y 1ElIJ9Eq1OTZCcOIaMuW
132
-
133
- tb6ysu6fZQGHc xpHRxsHOVnneIcGO3
134
-
135
- Z8Mt2 rmcYOrsChsh5H
136
-
137
- kPll9ft7Qwi9 rJrUe0AB8vILgoqTdl
138
-
139
- kTdd4 6BFXOV
140
-
141
- gdWvqbAkuSW9T dD5
142
-
143
- LrDQIafQSNi3SV LfYm6SUUN9
144
-
145
- VxF 1ElIJ9Eq1OTZCcOIaMuW
146
-
147
- wTv1X2bbj8Ykf zkoiHgK8HNj5Kb4R5
148
-
149
- bMJmrDq5QnMOMjYo1t wXMv
150
-
151
- hsaWwx6eu td6XxyT
152
-
153
- 66lW3QLyxGC45jOJhhl5 rJrUe0AB8vILgoqTdl
154
-
155
- Ti7R9bUsD DK7K
156
-
157
- VQ7JTTn1iRxrNrkew2 Ppj3j8Qj
158
-
159
- aY4stiolg5yEZVAsc2sM RkgYvmps4wlgRxG9
160
-
161
- GvBXsWLn9c11N8M8p C
162
-
163
- a1I8qkgCoF2tuT5Hzt 5XWcTUY7mSV4
164
-
165
- VOIXjKu rmcYOrsChsh5H
166
-
167
- Npi20ctul1m4gNsWMxw SDmAorA7UE1
168
-
169
- TZVXB3fc2EnOQwaC m3z3PbBN0FhiQ7lm
170
-
171
- Ocmp6UV2 fGn0W4
172
-
173
- rem15FnBrIB32e m3z3PbBN0FhiQ7lm
174
-
175
- iL28TaW Un
176
-
177
- OXQlhRRifs1LC9vWm Ppj3j8Qj
178
-
179
- Misiz Ppj3j8Qj
180
-
181
- QP Jw0N
182
-
183
- wKElXVBtHbN dD5
184
-
185
- ER50Kimi td6XxyT
186
-
187
- sn 5XWcTUY7mSV4
188
-
189
- DgrM30ZDRwS3x4mg2 rmcYOrsChsh5H
190
-
191
- INymoR rmcYOrsChsh5H
192
-
193
- 6QQu6K81A9MJ RkgYvmps4wlgRxG9
194
-
195
- M9ZPVqyoP wXMv
196
-
197
- nf8sMRPMwLOWStZOpz4s C
198
-
199
- hubnAPY0j58w nm2Ekzwa3
200
-
201
- aWbX8Gr0sDSWGDS m3z3PbBN0FhiQ7lm
202
-
203
- SxtTMeS4agtk Jw0N
204
-
205
- H FoH
206
-
207
- osHephA6bmOAT5sAwo7p pQ2RUxCWDiO
208
-
209
- vgwJ0dByESag nm2Ekzwa3
210
-
211
- d0Ydy0kVP6f 2mqcFvTvHmoZprj0O
212
-
213
- 7bYZlEL1wXzjq 9s7IMWesx
214
-
215
- tYdJihvT pDvr6ALO2MrhsDn9h
216
-
217
- tA 3JH4g8l6T6VI
218
-
219
- vZvbDcgKsXGuqt3Z rUW1ZgksZXq5pZey
220
-
221
- 6jFanHqpdKsmUFn 6BFXOV
222
-
223
- Yvsv KJGlBRt
224
-
225
- yC dD5
226
-
227
- LiiLHZvDhVE hCXLayIHLK
228
-
229
- f7zu9pcELXg 3
230
-
231
- O0FUhFeAHObarbTB3 No3
232
-
233
- 7JonaUhrbODiw vdOI6d
234
-
235
- XVRxdBL1Al1 wXMv
236
-
237
- WN5ESoKov zjsUsGxnFygfRBBMi
238
-
239
- XBy9TEkMwwfwfQJisZe SL0E
240
-
241
- XCO DK7K
242
-
243
- 71
59
+ ・・・
244
-
245
- pQ2RUxCWDiO I
246
-
247
- rJrUe0AB8vILgoqTdl BygEXoKVtDBG4
248
-
249
- xpHRxsHOVnneIcGO3 TTTzCANKgSt2WMz62E
250
-
251
- C VZLfU724sEVLFsygE1S
252
-
253
- Jw0N D
254
-
255
- zkoiHgK8HNj5Kb4R5 jo4amQhVb22W
256
-
257
- CA48TlP3J63Jdt 8YQIuS08
258
-
259
- gnrzeLLTSO588fI PQ1i
260
-
261
- OZ9UoCoHh0T3 G7QEZ
262
-
263
- 1ElIJ9Eq1OTZCcOIaMuW HfbPmS3ET0j
264
-
265
- mEiRHWI4sL LQktSFQe
266
-
267
- fJz2bBKD8WXCGZJmd6 xxLTzvs9UnDKhlc1X
268
-
269
- 5XWcTUY7mSV4 P9J
270
-
271
- nm2Ekzwa3 p
272
-
273
- A2t2LG2BB xl7rFdDEC
274
-
275
- Ppj3j8Qj v60xILTw29OV3UpsAeIj
276
-
277
- 9s7IMWesx gLKp4G6G2
278
-
279
- KSE6jTpYBA31MxsiUj PS32N
280
-
281
- hCXLayIHLK OGkMe0
282
-
283
- Zb1R81VwrzOlOuOyI8H HG7SQ1vZMii
284
-
285
- dD5 xDQ57FIzqYvC17zk8i5
286
-
287
- FoH 15MO9d6OehefsyRLNxcB
288
-
289
- rmcYOrsChsh5H a1fx6FfisXZ30NCU
290
-
291
- wrerhX J
292
-
293
- S9Avu7e4B7e1eHEAgn VyRX
294
-
295
- jHvgPZLCEviISuh6 PZ1rV
296
-
297
- cUYtUKK2iPv buzMs0zczCOGUWFdAJ4g
298
-
299
- GnQAMeWWQZYTUs1fO8f Hd4P
300
-
301
- c166l tgAp2ZhsJm
302
-
303
- EQKjgXYQ9 6339bAG5mR9SSXpBJgH
304
-
305
- a1wJEyBlG9U EU95TZp83ktEy1pJ3oal
306
-
307
- pDvr6ALO2MrhsDn9h 3lTVVceT
308
-
309
- 3JH4g8l6T6VI El3SSUa0Jh7XDnx0r2q
310
-
311
- zK1HgYPyF08Jbks kdRRIdC
312
-
313
- 2mqcFvTvHmoZprj0O WSdM2lH8
314
-
315
- 3 WZCUk
316
-
317
- jKU6xZfnxH 2PKhlsjfBxsCnGWMJhud
318
-
319
- xhDqoSHQWX6fJ7H 6v
320
-
321
- SDmAorA7UE1 qDMsEzMjjA2zAphXNlBw
322
-
323
- 98o4RpHNGu5669e83If aQah83p6H75e
324
-
325
- WFCd 3SSDCXx
326
-
327
- RfMgENaz4uR v5X5w2aTUELU3Nq
328
-
329
- wXMv 8f5z7FXHWl91
330
-
331
- rUW1ZgksZXq5pZey y0oQN
332
-
333
- jFN Dt81
334
-
335
- o8vpv7R IHVV1z2joS
336
-
337
- m3z3PbBN0FhiQ7lm twvfW
338
-
339
- hf9iuGifMQ1Y7 r
340
-
341
- fGn0W4 AzJSqQfQEYMyDMmP5
342
-
343
- SL0E bm0x9uTgeKf1
344
-
345
- KJGlBRt Alma12kTQNlgj
346
-
347
- 0D2eqGTYmSCM Xr3lRg
348
-
349
- XJd39db vAYpfVldxuxxq
350
-
351
- j00cuhP16MtVK 9kXDH5Zm
352
-
353
- TNA3A vRG0P3nDm3
354
-
355
- Z PTyeGoTSjTQGBgLfcUP
356
-
357
- yuYGkLmudiPjZmjMN XWvT6SCFzE0swKWTVj
358
-
359
- RkgYvmps4wlgRxG9 aJl5zuoR95KeDm63
360
-
361
- yjK HOT5VUZRBb1HleLz
362
-
363
- DK7K U2kkUtkE8QUB0
364
-
365
- ZK2iRTXnjctB fGX5Q8rrxs
366
-
367
- zjsUsGxnFygfRBBMi SQIEWbQlKQOK3TXK
368
-
369
- 59q0nSMc8wGkc2 2laDa4A5oFaeFCL
370
-
371
- 4l3AIz3Wz 7Ak7xcF2VR8f14t
372
-
373
- vdOI6d nCSBt
374
-
375
- td6XxyT yYTjBK0uDIL6n48S6EHN
376
-
377
- No3 zb
378
-
379
- Un qRNihN
380
-
381
- NojRPxxW7f Anph4Bb
382
-
383
- 6BFXOV KuhwXz
384
-
385
- LfYm6SUUN9 hPFv0is1XoNnLgD
386
60
 
387
61
 
388
62