匹配相同字母的单词

时间:2013-08-22 12:31:17

标签: c

我试图匹配两个单词,然后将它们打印出来,例如'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();
}

请真的需要帮助。很抱歉,如果我没有明白我的问题。

2 个答案:

答案 0 :(得分:5)

对两个字符串中的字母进行排序,然后比较它们是执行所需操作的简单方法之一。 (假设您熟悉排序)

它可能不是最有效的,但我再一次担心效率太高通常最好留在你有一个有效的解决方案和绩效指标之后。

如果你想要一些更有效的方法来检测两个单词是否是字谜,请查看Mats Petersson提供的链接,Optimizing very often used anagram function

答案 1 :(得分:0)

像这样的东西也可以工作..(对不起难看的读码,非常忙于别的东西)......

#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次搜索需要7188ms,因此每次搜索大约0.0599ms ...