-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp.c
104 lines (85 loc) · 1.94 KB
/
p.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#define LEN_SIZE 15
#define _DEBUG
int main(){
char str_0[100];
char **str_1;
int size_0 = 0;
int thread_num;
int i;
int count = 0;
double start_time, end_time;
FILE *fp = fopen("words.txt","r");
printf("Input the number of thread\n");
scanf("%d",&thread_num);
omp_set_num_threads(thread_num);
while(fscanf(fp,"%s",str_0) != EOF)size_0 += 1;
fclose(fp);
str_1 = (char **)malloc(sizeof(char *) * (size_0 + 1));
for(i = 0; i < size_0; i++){
str_1[i] = (char *)malloc(sizeof(char) * LEN_SIZE);
}
printf("The number of word is %d\n",size_0);
#ifdef DEBUG
printf("State 1\n");
#endif
fp = fopen("words.txt","r");
for(i = 0; i < size_0; i++){
fscanf(fp,"%s",str_1[i]);
//printf("%s\n",str_1[i]);
}
fclose(fp);
#ifdef DEBUG
printf("State 2\n");
printf("size_0 is %d\n",size_0);
#endif
start_time = omp_get_wtime();
fp = fopen("result.txt","w");
# pragma omp parallel for schedule(static) reduction(+:count)
for(i = 0; i < size_0; i++){
int j, k;
char l_0, l_1;
int len_0, len_1;
int flag;
len_0 = strlen(str_1[i]);
#ifdef DEBUG
printf("len_0 is %d i value is %d\n",len_0,i);
#endif
for(j = 0; j < size_0; j++){
len_1 = strlen(str_1[j]);
#ifdef DEBUG
if(i == j)printf("i, j, len, len %d %d %d %d\n",i,j,len_0,len_1);
#endif
if(len_0 == len_1){
flag = 0;
#ifdef DEBUG
printf("i j value is %d %d len size is %d %d\n",i,j,len_0,len_1);
#endif
for(k = 0; k < len_0; k++){
l_0 = str_1[i][k];
l_1 = str_1[j][len_0 - k - 1];
if(l_0 != l_1){
flag = 1;
}
}
if(flag == 0){
#ifdef DEBUG
printf("i and j is %d %d\n",i,j);
#endif
count = count + 1;
# pragma omp critical(write)
fprintf(fp,"%s\n",str_1[i]);
break;
}
}
}
}
fclose(fp);
end_time = omp_get_wtime();
printf("Number of Palindromic is %d\n",count);
printf("Elapsed time %f sec.\n",end_time - start_time);
return 0;
}