簡體   English   中英

匹配相同字母的單詞

[英]Matching words with same letters

我正在嘗試匹配兩個單詞,然后將它們打印出來,例如“ act”和“ cat”中包含“ a”,“ c”和“ t”,以便它們匹配。 這是我的代碼:

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

main()
{
  FILE        *fptr;
  char        words[100], input[100], store[1000][100] 
  char        ch
  int         i,j,k,z,b,*ptr;

  ptr = &b;

  fptr = fopen("d:\\words.txt","r");
  if (fptr == NULL)
  {
           printf("Could not open file");
           exit(1);
  }

  printf("Enter the scrambled word: ");
  fflush(stdin);
  fgets (input,sizeof(input),stdin);

  i = 0;
  while (fgets(words,sizeof(words),fptr) != NULL)
  {     
        if (strlen(input) == strlen(words))
        {
           strcpy(store[i],words);
           ++i;
        }
  }
  //this is where the problem is:
  /*am trying to match the letters in two words, if they don't match then store 1 in b,
  if b=0 then print out the word which matched with string 'input'*/
  for(z = 0; z < 1000; ++z)
  {
        b = 0;
        for(j = 0; j < strlen(input); ++j)
        {
              for(k = 0; k < strlen(store[z]); ++k)
              {
                    if(input[j] != store[z][k])
                        *ptr = 1;          
              }
        }
        if(*ptr == 0)
        {          
                   printf("Word #%2d is: %s\n", z, store[z]);   
        }
  }



  fflush(stdin);
  getchar();
}

請我真的需要幫助。 很抱歉,如果我沒有明確說明我的問題。

對兩個字符串中的字母進行排序,然后進行比較,這是完成所需操作的最簡單方法之一。 (假設您熟悉排序)

它可能不是最有效的,但我再說一遍,通常最好不要擔心效率,直到您有了可行的解決方案和性能指標為止。

如果您想使用一些更有效的方法來檢測兩個單詞是否為字謎,請查看Mats Petersson提供的鏈接,“ 最常用的字謎功能的優化”。

像這樣的事情也可以工作..(對不起,丑陋的閱讀代碼,非常忙於其他事情)...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>

#include <string>
#include <list>
#include <map>
#include <sstream>
#include <algorithm>

using namespace std;

map< string, list<string> > items;
int c = 0;

void readFile() {
        FILE * f = fopen( "c:\\t\\words.txt", "r" );
        fseek(f, 0L, SEEK_END);
        int size = ftell(f);
        fseek(f, 0L, SEEK_SET);
        char * data = (char*)malloc(size);
        fread(data, size, 1, f);

        string s = string(data);
        istringstream reader(s);
        while(reader) {
            string sub;
            reader >> sub;

            string original = sub;
            sort( sub.begin(), sub.end() );

            items[sub].push_back(original);        
            c++;
        }


        free(data);
        fclose(f);
}

bool     check( const string & v ) {
    string requestStr = v;
    sort( requestStr.begin(), requestStr.end() );
    printf("Requested: %s [%s]\n", v.c_str(), requestStr.c_str());

    if (items.find(requestStr) == items.end()) {
        printf("Not found\n");
        return false;
    }

    list<string>::iterator it = items[requestStr].begin();

    while (it != items[requestStr].end()) {
        printf("Found: %s\n", (*it).c_str());       
        it++;
    }
}

int main(int argc, char ** argv) {
    long t1 = GetTickCount();
    readFile();
    printf("Read wordlist (%i): %li ms\n", c, GetTickCount() - t1 );

    string str = "holiday";
     t1 = GetTickCount();
    check(str);
    printf("Time: %li ms\n",  GetTickCount() - t1 );


    str = "tac";
     t1 = GetTickCount();
    check(str);
    printf("Time: %li ms\n",  GetTickCount() - t1 );

    str = "dfgegs";
     t1 = GetTickCount();
    check(str);
    printf("Time: %li ms\n",  GetTickCount() - t1 );

}

109000字文件的搜索結果

Read wordlist (109583): 5969 ms
Requested: holiday [adhiloy]
Found: holiday
Time: 0 ms
Requested: tac [act]
Found: act
Found: cat
Time: 0 ms
Requested: dfgegs [defggs]
Not found
Time: 0 ms

120000次搜索需要7188毫秒,因此每次搜索大約需要0.0599毫秒...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM