English 中文(简体)
执行一个可溶解的反应堆
原标题:Implementing a scrabble solver
  • 时间:2010-05-28 19:41:52
  •  标签:
  • c
  • perl

在我问任何事情之前,让每个人都知道,这是幸运的,我已经公布了我迄今为止的所有法典;随着事情的固定/执行,将会把更多的事情放在一边,对长期职位感到担忧!

我在此有两个问题,我将把我的所有法典放在后面。

  1. 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);
  2. 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(&current_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;

}
问题回答

仅对您的Perl提出一些想法。

无理由进行大散散射。 您可以按照以下一点开始:

my %w = map { $_ => {} }  a .. z ;

但是,完全没有理由 in,如果你说:

$w{$1}{$_} = 1 if /^(w)/;

但你有错误,如果一字从封顶信开始,就会进入错误的关键。 如果你想要追捕这些错误,你可以使用哈希姆:Util s lock_keys,以防止在你身上添加新的钥匙。 用<条码>lc或<条码>使你的言词规范化,以强制适用正确情况。

你与你的Perl会提出一些其他次要问题。 而且,由于你正在处理(推定)大量档案,为什么要保留所有字句?

#!/usr/bin/perl
use strict;
use warnings;

use IO::Handle;

open my $fh,  < , $wordlist_path 
    or die "Error opening word list  $wordlist  - $!
";

# Open a handle for each target file.    
my %handle = map { 
    open my $fh,  > , $_ 
        or die "Error opening sublist $_ - $!
";
    $_ => $fh;
}  a .. z ;

while( my $word = <$fh> ) {

    $word = clean_word( $word );

    my $first_letter = substr $word, 0, 1;

    $handle{$first_letter}->print( "$word
" );
}

sub clean_word {
    my $word = shift;

    chomp $word;
    $word = lc $word;

    $word =~ s/^s*//;
    $word =~ s/s*$//;

    return $word;
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签