簡體   English   中英

Word解擾程序-C ++

[英]Word Unscrambling Program - C++

嗨,我正在使用一個程序來解開一組字母並輸出可以從該組字母中得到的所有單詞,例如:如果我輸入了字母“ vlei”,該程序將輸出“ live”,“ evil” ”和“惡意”。

到目前為止,我已經在互聯網上略微了解了這一奇特之處,並且在這一點上我無法找到與我的技能水平相關的特定問題(2級菜鳥)。

到目前為止,我已經根據給定的字母進行了所有可能的組合。 排除少於7個字母的字母,這是一個問題。

這是我到目前為止的代碼:

string letter;
char newWord[7];

    int main()
{

cout << "Type letters here: ";
cin >> letter;


for(int i = 0 ; i < 7 ; i++)
{   
    for(int j = 0 ; j < 7 ; j++)
    {
        for(int k = 0 ; k < 7 ; k++)
        {
            for(int l = 0 ; l < 7 ; l++)
            {
                for(int m = 0 ; m < 7 ; m++)
                {
                    for(int n = 0 ; n < 7 ; n++)
                    {
                        for(int o = 0 ; o < 7 ; o++)
                        {

                            sprintf(newWord, "%c%c%c%c%c%c%c", letter[i], letter[j], letter[k], letter[l], letter[m], letter[n], letter[o]);

                        }
                    }
                }
            }
        }
    }
}

return 0;

}

我想知道是否有人有類似的經驗,可以提供提示或建議。

具體來說,我遇到的困難是如何讀取.txt文件以用作將單詞進行比較的字典。 另外,我在使用strcmp()遇到了麻煩,這是我打算用來將加擾的單詞與字典進行比較的。 因此,如果還有其他也許更簡單的方法來比較兩個字符串,那將不勝感激。

提前致謝。


嗨,大家好,我剛剛完成我的計划,希望它可以對其他人有所幫助。 非常感謝您的所有幫助。

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <array>

using namespace std;



//declaring variables
int i;
int scores[531811]; //array for scores of found words
string wordlist[531811]; //array for found matched words
string word[531811]; //array of strings for dictionary words about to be read it
string tester;//string for scrambled letters that will be read in

int scorefinder(string scrab) //SCORE FINDER FUNCTION
{
    int score = 0;
    int x = 0;
    int j = 0;
    while (scrab[j])
    {
        char ltr = toupper(scrab[j]); //converts to all caps

        //assings values to each letter and adds it to itself
        if(ltr == 'A' || ltr == 'E' || ltr == 'I' || ltr == 'L' || ltr == 'N' || ltr == 'O' || ltr == 'R' || ltr == 'S' || ltr == 'T' || ltr == 'U')
            x += 1;
        else if(ltr == 'D' || ltr == 'G')
            x += 2;
        else if(ltr == 'B' || ltr == 'C' || ltr == 'M' || ltr == 'P')
            x += 3;
        else if(ltr == 'F' || ltr == 'H' || ltr == 'V' || ltr == 'W' || ltr == 'Y')
            x += 4;
        else if(ltr == 'K')
            x += 5;
        else if(ltr == 'J' || ltr == 'X')
            x += 8;
        else if(ltr == 'Q' || ltr == 'Z')
            x += 10;
        ++j;
    }
    score = x;
    return score;
}

int main () {

//READS IN DICTIONARY
    ifstream file("words.txt"); //reads in dictionary
    if (!file.is_open()){ //checks if file is being NOT read correctly
        cout << "BROEKN \n"; //prints error message if so
    }
    if(file.is_open()){ //checks if file IS being read correctly
        for(int i = 0; i < 531811; i++){ 
            file >> word[i]; //read in each word from the file and 
        }   //assigns each to it's position in the words array
    }
//END OF READ IN DICTIONARY

    cout << "Enter scrambled letters: ";
    cin >> tester; //reads in scrambled letters

    sort(tester.begin(),tester.end()); //sorts scrambled letters for next_permutation
    while (next_permutation(tester.begin(),tester.end())){  //while there are still permutations available
        for(i=0;i<531811;i++){
            if ( is_permutation (word[i].begin(),word[i].end(), tester.begin())){
                wordlist[i] = word[i]; //assigns found word to foundword array
                scores[i] = scorefinder(word[i]); //assigns found word score to foundscore array
            }
        }
    }

    //PRINTS OUT ONLY MATCHED WORDS AND SCORES
    for(i=0;i<531811;i++){
        if(scores[i]!=0){
            cout << "Found word: " << wordlist[i] << " " << scores[i] << "\n";
        }
    }
}

好吧,您需要進行某種比較。 C ++不知道,英語中的正確單詞是什么。 因此,您可能需要一個單詞表。 然后,您可以進行Brutforce(此刻您正在做的事情),直到找到匹配項為止。

為了比較您的強求結果,您可以使用.txt和盡可能多的英語單詞。 然后,您必須使用FileStream遍歷每個單詞並將其與brutforce結果進行比較。

成功解讀一個單詞后,您應該重新考慮您的解決方案。 如您所見,您只能使用特定數量的字符,這並不是很好。

對於初學者來說,這聽起來像是一個有趣的任務;)

嘗試這個 :

# include <stdio.h>

/* Function to swap values at two pointers */
void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

/* Function to print permutations of string
   This function takes three parameters:
   1. String
   2. Starting index of the string
   3. Ending index of the string. */
void permute(char *a, int i, int n) 
{
   int j; 
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j)); //backtrack
       }
   }
} 

/* Driver program to test above functions */
int main()
{
   char a[] = "vlei";  
   permute(a, 0, 3);
   getchar();
   return 0;
}

假設您在Internet上以純文本文件的形式找到了單詞列表,則可以將所有單詞首先加載到用於字符串的向量中。

ifstream word_list_file("word_list.txt");
string buffer;
vector<string> all_words;
while (getline(word_list_file, buffer))
    all_words.push_back(buffer);

然后,我們想將輸入字母與all_words的每個條目進行all_words 我建議使用std::is_permutation 無論順序如何,它都會比較兩個序列。 但是,當兩個序列的長度不同時,可能會遇到麻煩,因此請先自己比較長度。

// Remember to #include <algorithm>
bool match(const string& letters, const string& each_word)
{
    if (letters.size() != each_word.size())
        return false;

    return is_permutation(begin(letters), end(letters), begin(each_word));
}

請注意,我尚未測試我的代碼。 但這就是想法。


響應評論的編輯:

簡而言之,只需使用std::string ,而不是std::array 或直接復制我的match函數,然后調用它。 對於您的情況,這將更容易。

細節:

std::is_permutation可以與任何容器和任何元素類型一起使用。 例如:

#include <string>
#include <array>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;

int main()
{
//Example 1
    string str1 = "abcde";
    string str2 = "ecdba";
    is_permutation(begin(str1), end(str1), begin(str2));

//Example 2
    array<double, 4> array_double_1{ 4.1, 4.2, 4.3, 4.4 };
    array<double, 4> array_double_2{ 4.2, 4.1, 4.4, 4.3 };
    is_permutation(begin(array_double_1), end(array_double_1), begin(array_double_2));

//Example 3
    list<char> list_char = { 'x', 'y', 'z' };
    string str3 = "zxy";
    is_permutation(begin(list_char), end(list_char), begin(str3));

// Exampl 4
    short short_integers[4] = { 1, 2, 3, 4 };
    vector<int> vector_int = { 3, 4, 2, 1 };
    is_permutation(begin(list_char), end(list_char), begin(str3));

    return 0;
}
  • 示例1使用std::string作為char的容器,這正是我的match函數的工作方式。
  • 示例2使用大小為double的兩個array s。
  • 示例3甚至使用了兩種具有相同元素類型的不同容器。 (您是否聽說過“ std :: list”?沒關系,請先關注我們的問題。)
  • 示例4甚至更陌生。 一個容器是舊式的原始數組,另一個是std::vector 還有兩種元素類型, shortint ,但它們都是整數。 shortint之間的確切區別在這里不相關。)

但是,所有四種情況都可以使用is_permutation 非常靈活。

可以通過以下事實實現靈活性:

  1. is_permutation不完全是一個函數。 它是一個功能模板,這是一種語言功能,可以根據傳遞給它的數據類型生成新功能。
  2. 容器和is_permutation算法互不認識。 他們通過稱為“迭代器”的中間人進行通信。 beginend函數一起為我們提供了一對表示元素“范圍”的迭代器。

需要更多的研究來理解這些事實。 但是總體思路並不難。 同樣,這些事實也適用於標准庫中的其他算法。

暫無
暫無

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

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