/home/toolbox/public_html/solutions/127/12724/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include <math.h>
8 #include <stdint.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14
15 /* fprintf(stderr, "functionName: message", varslist); */
16
17 /*
18 * Author: Isaac Traxler
19 * Date: 2015-09-30
20 * Purpose: fun
21 * Problem: 12724
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28 #define MAX_WORD_LENGTH 42
29 #define MAX_WORDS 101
30 #define MAX_LINE_LENGTH 4097 /* did not make buffer big enough first time. 50 words by 40 length plus 39 spaces */
31
32 int numberOfTimes;
33 int wordCount;
34 int lineCount;
35 char buff[MAX_LINE_LENGTH];
36 char words[MAX_WORDS][MAX_WORD_LENGTH];
37 char sorted[MAX_WORDS][MAX_WORD_LENGTH];
38
39 void init()
40 {
41 /* FUNCTION init */
42 scanf("%d ", &numberOfTimes);
43 } /* FUNCTION init */
44
45 void dump()
46 {
47 /* FUNCTION dump */
48 } /* FUNCTION dump */
49
50 int compAscend(const char * a, const char * b)
51 {
52 /* FUNCTION compare function for qsort */
53 int toReturn = 0;
54
55 if (*a < *b)
56 toReturn = -1;
57 else if (*a > *b)
58 toReturn = 1;
59 return toReturn;
60 } /* FUNCTION compare function for qsort */
61
62
63 void getInput()
64 {
65 /* FUNCTION getInput */
66 int i;
67
68 scanf(" %d %d ", &wordCount, &lineCount);
69 for (i=0; i<wordCount; i++)
70 {
71 /* for each word */
72 scanf(" %s ", words[i]);
73 DEBUG printf("%d: [%s]\n", i, words[i]);
74 strcpy(sorted[i], words[i]);
75 qsort(sorted[i], strlen(sorted[i]), sizeof(char), compAscend);
76 DEBUG printf("getInput: %d: [%s] [%s]\n", i, words[i], sorted[i]);
77 } /* for each word */
78 } /* FUNCTION getInput */
79
80 void printWord(char wrd[])
81 {
82 /* FUNCTION printWord */
83 int i;
84 DEBUG printf("printWord: wrd=[%s]\n", wrd);
85 for (i=0; i<wordCount; i++)
86 {
87 /* compare words */
88 DEBUG printf("printWord; comparing %d: [%s] [%s]\n", i, wrd, sorted[i]);
89 if (0 == strcmp(wrd, sorted[i]))
90 {
91 /* look for word */
92 printf("%s", words[i]);
93 i = wordCount;
94 } /* look for word */
95 } /* compare words */
96 } /* FUNCTION printWord */
97
98 void processLine()
99 {
100 /* FUNCTION processLine */
101 char *ptr;
102 char wrd[MAX_WORD_LENGTH];
103 int n;
104 int first = TRUE;
105
106 ptr=buff;
107 while ('\0' != *ptr)
108 {
109 /* parse line */
110 sscanf(ptr, " %s %n", wrd, &n); /* n receives how many bytes were consumed */
111 ptr = ptr + n; /* advance pointer across line */
112 DEBUG printf("processLine: wrd=[%s] ", wrd);
113 qsort(wrd, strlen(wrd), sizeof(char), compAscend);
114 if (first)
115 {
116 first = FALSE;
117 }
118 else
119 {
120 printf(" "); /* print a space between words */
121 }
122 DEBUG printf("wrd=[%s]\n", wrd);
123 printWord(wrd);
124 } /* parse line */
125 printf("\n");
126 } /* FUNCTION processLine */
127
128 void process()
129 {
130 /* FUNCTION process */
131 int i;
132
133 for (i=0; i<lineCount; i++)
134 {
135 /* for each line of the test */
136 fgets(buff, sizeof(buff), stdin); /* get line */
137 buff[strlen(buff) - 1] = 0; /* repalce EOL */
138 DEBUG printf("line [%s]\n", buff);
139 processLine();
140 } /* for each line of the test */
141 } /* FUNCTION process */
142
143 int main()
144 {
145 /* main */
146 int i;
147
148 init();
149 for (i=1; i<=numberOfTimes; i++)
150 {
151 /* while */
152 printf("Case #%d:\n", i);
153 getInput();
154 process();
155 } /* while */
156
157 return EXIT_SUCCESS;
158 } /* main */
159
160