在我问任何事情之前,让每个人都知道,这是幸运的,我已经公布了我迄今为止的所有法典;随着事情的固定/执行,将会把更多的事情放在一边,对长期职位感到担忧!
我在此有两个问题,我将把我的所有法典放在后面。
- I can t seem to figure out why when inputting 12+ letters with some of the same letters,I am getting several duplicates as my accepted int is in place to avoid duplicates (works for the most part);
- given an input of up to 26 letters and an nxn board (having some letters filled in already), output all possible word combos that fit in valid spots. Any advice as to how to go about this (the board would be a 2-d array 1 char space in each for 1 letter)
现在 它只是一个基于文字的方案,接受最多26封信,并产生200K字+字典上的所有有效字句:
下面的C方案要求将起诉人列入26份档案,其中载列从每份档案中每一封信开始的所有字句(所有字在档案中......)在下文中张贴。
口头答辩(c)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM_CHARS 26
#define MAX_WORD_LEN 20
#define WORDS_PER_LINE 12
/* Character link structure */
typedef struct char_link
{
struct char_link **cl; /* All of this links possible next characters */
short eow; /* END OF WORD (means this is the last letter of a valid word */
} CHARLINK;
/* Global found word count, used for printing
char. */
unsigned short gwc = 0;
CHARLINK * _init_link(CHARLINK **link)
{
short i;
(*link)->cl = (CHARLINK **) malloc(NUM_CHARS * sizeof(CHARLINK *));
for (i = 0; i < NUM_CHARS; i++)
(*link)->cl[i] = NULL;
(*link)->eow = 0;
return (*link);
}
void _build_char_link(CHARLINK *link)
{
FILE *fp;
char *ptr, file[2];
CHARLINK *current_link = NULL;
char line_buffer[MAX_WORD_LEN];
unsigned short size = 0;
static short letter_index = 0;
int current_letter = 0;
sprintf(file, "%c", letter_index + a );
current_link = _init_link(&link);
if (fp = fopen(file, "r"))
{
while (fgets(line_buffer, MAX_WORD_LEN, fp) > 0)
{
/* Skip letter_index */
ptr = line_buffer + 1;
while(*ptr && (*ptr !=
&& *ptr !=
))
{
current_letter = (int)(*ptr - a );
/* Create and jump to new link */
if (!current_link->cl[current_letter])
{
current_link->cl[current_letter] = (CHARLINK *) malloc (sizeof(CHARLINK));
current_link = _init_link(¤t_link->cl[current_letter]);
}
/* Jump to existing link */
else
current_link = current_link->cl[current_letter];
ptr++;
}
current_link->eow = 1;
/* Reset our current_link pointer to the letter_index link */
current_link = link;
}
fclose(fp);
}
else
printf("Warning: Couldn t import words for letter: %s
", file);
letter_index++;
}
void _draw_tree(CHARLINK *link, short letter, short depth)
{
short i, tmp;
if (!depth)
{
printf("Data for letter %c
", letter + a );
printf("%c
", letter + a );
}
for (i = 0; i < NUM_CHARS; i++)
{
if (link->cl[i])
{
tmp = depth;
while (tmp-- >= 0)
printf(" ");
printf("%c(%d)
", i + a , link->cl[i]->eow);
_draw_tree(link->cl[i], letter, depth + 1);
}
}
}
void _get_possible_words(CHARLINK *link, char *prefix, char *letters, unsigned int input_len, unsigned int depth)
{
short i, len, j;
unsigned int attempted = 0x00000000;
if (link->eow)
{
printf(" %s", prefix);
if (++gwc == WORDS_PER_LINE)
{
printf("
");
gwc = 0;
}
}
len = strlen(prefix);
for (i = 0; i < input_len; i++)
{
if (letters[i])
{
j = (1 << (letters[i] - a ));
if (!(j & attempted) && link->cl[letters[i] - a ])
{
prefix[len] = letters[i];
letters[i] = ;
_get_possible_words(link->cl[prefix[len] - a ], prefix, letters, input_len, depth + 1);
letters[i] = prefix[len];
prefix[len] = ;
}
attempted |= j;
}
}
}
int main(int argc, char *argv[])
{
short i;
/* 26 link structures for a-z */
CHARLINK root_nodes[NUM_CHARS];
printf("Building structures ");
for (i = 0; i < NUM_CHARS; i++)
{
_build_char_link(&root_nodes[i]);
printf(". ");
}
printf("Done!
");
/* Debug, what do our trees look like? */
//for (i = 0; i < NUM_CHARS; i++)
// _draw_tree(&root_nodes[i], i, 0);
for(;;)
{
short input_len = 0;
unsigned int j = 0, attempted = 0x00000000;
char input[26] = {0};
char letters[26] = {0};
char prefix[26] = {0};
printf("Enter letters ( 0 to exit): ");
gets(input); /* Yay buffer overflow */
if (input[0] == 0 ) break;
sprintf(letters, "%s", input);
input_len = strlen(input);
for (i = 0; i < input_len; i++)
{
j = (1 << (input[i] - a ));
if (!(j & attempted))
{
prefix[0] = input[i];
letters[i] = ;
_get_possible_words(&root_nodes[prefix[0] - a ], prefix, letters, input_len, 1);
letters[i] = input[i];
attempted |= j;
}
}
printf("
");
}
return 255;
}
档案分离:
#!/usr/bin/perl
open(FH, "< words.txt");
my %w = map { $_ => {} } a .. z ;
while (<FH>)
{
s/s+$//;
$w{lc $1}->{lc $_} = 1 if /^(w)/;
}
foreach my $l ( keys %w )
{
open (OUT, "> $l");
foreach my $a ( keys %{$w{$l}} )
{
print OUT "$a
";
}
close OUT;
}