Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

Searching Quickly

123.c

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int num_ignore;
char ignore[50][20];

int num_titles;
struct title {
	int num_words;
	char words[15][20];
} titles[200];

int num_words;
char *words[3000];

int
comp(const void *a, const void *b)
{
	char *str1 = *(char **)a;
	char *str2 = *(char **)b;

	return strcmp(str1, str2);
}

char *
caps(char * str)
{
	int i;
	static char result[100];
	
	for(i=0; i<strlen(str); i++) {
		result[i] = toupper(str[i]);
	}
	result[i]=0;
	return result;
}

void
calc(void)
{
	int i,j,k,l;

	for(i=0; i<num_titles; i++) {
		for(j=0; j<titles[i].num_words; j++) {
			int skip=0;
			for(k=0; k<num_ignore; k++) {
				if (!strcmp(ignore[k], titles[i].words[j])) {
					skip=1;
					break;
				}
			}
			if (!skip) {
				words[num_words++] = titles[i].words[j];
			}
		}
	}
	qsort(words, num_words, sizeof(char*), comp);
	for(i=0; i<num_words; i++) {
		if (i>0 && !strcmp(words[i], words[i-1])) {
			continue;
		}
		for(j=0; j<num_titles; j++) {
			for(k=0; k<titles[j].num_words; k++) {
				if (!strcmp(words[i], titles[j].words[k])) {
					for(l=0; l<titles[j].num_words; l++) {
						if (l!=0) {
							printf(" ");
						}
						if (l==k) {
							printf("%s", caps(titles[j].words[l]));
						} else {
							printf("%s", titles[j].words[l]);
						}
					}
					printf("\n");
				}
			}
		}
	}
}

int
main(void)
{
	int i,j,k;
	char str[11];
	char buf[10000];

	num_ignore=0;
	num_titles=0;

	while(fgets(buf, 10000, stdin)) {
		sscanf(buf, "%s", str);
		if (!strcmp(str,"::")) {
			break;
		}
		for(i=0; i<strlen(str); i++) {
			str[i] = tolower(str[i]);
		}
		strcpy(ignore[num_ignore++], str);
	}
	i=0;
	while(fgets(buf, 10000, stdin)) {
		titles[i].num_words = sscanf(buf,
			"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
			titles[i].words[0], titles[i].words[1], titles[i].words[2],
			titles[i].words[3], titles[i].words[4], titles[i].words[5],
			titles[i].words[6], titles[i].words[7], titles[i].words[8],
			titles[i].words[9], titles[i].words[10], titles[i].words[11],
			titles[i].words[12], titles[i].words[13], titles[i].words[14]);
			for(j=0; j<titles[i].num_words; j++) {
				for(k=0; k<strlen(titles[i].words[j]); k++) {
					titles[i].words[j][k] = tolower(titles[i].words[j][k]);
				}
			}
		i++;
	}
	num_titles=i;
	calc();

	exit(0);
}