Skip to content

Instantly share code, notes, and snippets.

@fsouza
Last active December 10, 2015 16:49
Show Gist options
  • Save fsouza/4463848 to your computer and use it in GitHub Desktop.
Save fsouza/4463848 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cilk
#include <cilk/cilk.h>
#else
#define cilk_spawn
#endif
#define readline(buf) do { \
int j, c; \
for(j = 0; (c = getchar()) != '\n' && c != EOF; ++j) { \
buf[j] = c; \
} \
buf[j] = '\0'; \
} while(0)
#define readinput(input, output, n, limit, ofactor) do { \
char buf[8]; \
int i; \
readline(buf); \
n = atoi(buf); \
input = (char **)emalloc(n * sizeof(char *)); \
output = (char **)emalloc(n * sizeof(char *)); \
for(i = 0; i < n; ++i) { \
input[i] = (char *)emalloc(limit * sizeof(char)); \
readline(input[i]); \
output[i] = (char *)emalloc(ofactor * limit * sizeof(char)); \
output[i][0] = '\0'; \
} \
} while(0)
void *
emalloc(size_t s)
{
void *v;
v = malloc(s);
if(v == NULL) {
printf("Out of memory.\n");
exit(1);
}
return v;
}
void
free_streams(char **input, char **output, int n)
{
int i;
for(i = 0; i < n; ++i) {
free(input[i]);
free(output[i]);
}
free(input);
free(output);
}
char letters[26] = {
'y', 'h', 'e', 's', 'o', 'c', 'v', 'x', 'd', 'u', 'i', 'g', 'l',
'b', 'k', 'r', 'z', 't', 'n', 'w', 'j', 'p', 'f', 'm', 'a', 'q',
};
void
translate_line(char *iline, char *oline)
{
int i, length;
length = strlen(iline);
for(i = 0; i < length; ++i) {
if(iline[i] == ' ') {
oline[i] = iline[i];
} else {
oline[i] = letters[iline[i] - 'a'];
}
}
oline[i] = '\0';
}
void
translate(char **input, char **output, int n)
{
int i;
for(i = 0; i < n; ++i) {
cilk_spawn translate_line(input[i], output[i]);
}
}
int
main(void)
{
char **input, **output;
int i, n;
readinput(input, output, n, 100, 1);
translate(input, output, n);
for(i = 0; i < n; ++i) {
printf("Case #%d: %s\n", i+1, output[i]);
}
free_streams(input, output, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment