[英]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.