2013-08-22 32 views
0

匹配的话,我想匹配的两个词,然后打印出来e.g“行为”和“猫”有'一个,对它们的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(); 
} 

请真的需要帮助。对不起,如果我没有明确表示我的问题。

+5

你的问题是什么? – trojanfoe

+1

你在你的代码中说“这是问题所在”,但你不会告诉我们你的问题是什么,它与你的期望有什么不同。 –

回答

5

排序两个字符串中的字母,然后比较它们是做你需要什么的更简单的方法之一。 (假设你熟悉排序)

它可能不是最有效的,但我还是那句话,太担心效率通常是最好的留到你有一个有效的解决方案和性能指标之后。

如果要检测一些更有效的方法,如果两个词是字谜,请通过垫彼得森提供的链接,Optimizing very often used anagram function

+5

在下面的问题/答案中发现,排序不是最有效的方法。但是,是的,这是一个可行的方法。 http://stackoverflow.com/questions/18123959/optimizing-very-often-used-anagram-function/18124989#18124989 –

+0

@MatsPetersson感谢您的链接:) –

+0

完全用草席彼得森同意。在重复调用此过程的情况下,优先使用投票数组实现,复杂度与字符串长度和字母大小成线性关系。 – Bentoy13

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,所以周围每一台S 0.0599ms earch ...

+0

这是c语言吗?如果是这样,我只是一个初学者,抱歉没有得到它。 – Harith

+0

这是Ñ++,不完美,但给出了一个想法,所以你阅读所有单词,按字词排序字母,并将排序的单词存储在数组中,实际上是在地图中..因为您将在地图数组中重复每个“值”本身,它包含看起来都一样的话整理后..所以以后你只需要获得一个字,也进行排序的字母,并使用二进制搜索在地图包含这样的关键和打印效果 – evilruff

+0

你可以删除看,其只是GetTickCount的()函数来完成基本的观测值 – evilruff

相关问题