-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
118 lines (102 loc) · 2.59 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <inttypes.h>
#include <string.h>
#include <time.h>
#define MAX_ROW_SIZE 1000002
#define LETTERS_CNT 26
FILE * finput, * foutput;
const char * input_file_name = "input.txt";
const char * output_file_name = "output.txt";
char ** a;
int32_t ** letters;
uint32_t out_cnt = 0;
char ** create_char_array(int rows, int cols){
char ** p;
int i;
p = (char **)malloc(sizeof(char*)*rows);
for(i=0; i<rows; i++){
p[i]=(char *)malloc(sizeof(char)*cols);
}
return p;
}
char ** erase_char_array(int rows, char ** p){
int i;
for(i=0; i<rows; i++){
free(p[i]);
}
free(p);
return NULL;
}
int32_t ** create_int32_t_array(int rows, int cols){
int32_t ** p;
int i;
p = (int32_t **)malloc(sizeof(int32_t*)*rows);
for(i=0; i<rows; i++){
p[i]=(int32_t *)malloc(sizeof(int32_t)*cols);
}
return p;
}
char ** erase_int32_t_array(int rows, int32_t ** p){
int i;
for(i=0; i<rows; i++){
free(p[i]);
}
free(p);
return NULL;
}
int read_input(void)
{
char * estr;
int i;
finput = fopen(input_file_name, "r");
if(finput == NULL)
return (EXIT_FAILURE);
for (i=0; i<2; i++){
estr = fgets(a[i], MAX_ROW_SIZE, finput);
if (estr == NULL) {
exit(EXIT_FAILURE);
}
}
fclose(finput);
return 0;
}
int write_output(void)
{
foutput = fopen(output_file_name, "w");
if(foutput == NULL)
return (EXIT_FAILURE);
printf("%" PRIu32 "\n", out_cnt);
fprintf(foutput, "%" PRIu32 "\n", out_cnt);
fflush(foutput);
fclose(foutput);
return 0;
}
int main()
{
a = create_char_array(2, MAX_ROW_SIZE);
letters = create_int32_t_array(2, LETTERS_CNT);
read_input();
// printf ("I have read:\n%s%s",a[0],a[1]);
int i, j;
char * t;
for (i=0; i<2; i++) {
for (j=0; j<LETTERS_CNT; j++)
letters[i][j] = 0;
}
for (i=0; i<2; i++)
for (t = a[i]; *t != '\0'; t++)
letters[i][(int)(*t) - (int)('A')]++;
for (i=0; i<2; i++)
for (j=0; j<LETTERS_CNT; j++)
printf ("%c[%i] - %d\n",(char)(j+(int)('A')),i,letters[i][j]);
for (j=0; j<LETTERS_CNT; j++) {
out_cnt += abs(letters[0][j] - letters[1][j]);
printf ("%c - %d\n",(char)(j+(int)('A')),abs(letters[0][j] - letters[1][j]));
}
write_output();
erase_char_array(2, a);
erase_int32_t_array(2, letters);
return 0;
}