给定一些文本输入,找到最大的字谜组

Posted

技术标签:

【中文标题】给定一些文本输入,找到最大的字谜组【英文标题】:Given some text input, find the largest anagram groups 【发布时间】:2016-06-18 22:08:37 【问题描述】:

我一直在处理一项作业,需要输入包含由小写字母字符组成的单词,并用空格(或换行符)分隔。它以“STOP”一词结束。您可以假设不会超过 100 个单词,并且每个单词的长度不超过 20 个字符。

输出 5 个最大的字谜组。如果少于 5 个组,则全部输出。按减小大小对组进行排序。按字典顺序最小的元素按字典顺序断开联系。对于每个组输出,打印其大小及其成员词。按字典顺序对成员词进行排序,并且只打印一次equalwords。

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

void ReadWord(char [21]);
void SortWord(char [21]);
void AssignAnagramGroup(int);
void SortWordList();
void CalculateTopFiveGroups();
int isAnagram(char [21], char [21]);

char wordlist[100][21];  //stores list of words
int anagramgroup[100];  //for each word, store the group id it belongs to
int anagramgroupcount=1; //how many groups do we have
int anagramgroupsize[101];  //for each anagramgroup, we store size
int wordcount=0; //how many words were in the list
int largestfive[5]=0; //stores top 5 anagramgroups
int groupsizeorder[100]; //array to hold the groupsize in corresponding word placement
int main ()

 char word[21];
 int done = 0;
 int i, j, k;
 int x=1;
 do
 
    ReadWord(word);
        if (strcmp(word, "STOP")==0 || wordcount > 99)
            break;

    strcpy(wordlist[wordcount++], word);
    AssignAnagramGroup(wordcount-1);

while(!done);

for (i=0; i<wordcount; i++)
    printf ("ReadWord: Wordlist[%d]=\"%s\"      AnagramGroup=%d\n", i, wordlist[i], anagramgroup[i]);
    printf ("WordList size=%d words\n", wordcount);
    for (i=1; i<=5; i++)
        if (anagramgroupsize[i]!=0)
            printf ("Anagram Group %d of size %d\n",    i, anagramgroupsize[i]);
    printf ("\n");

    SortWordList();
    CalculateTopFiveGroups();

for (i=0; i<wordcount; i++)
    printf ("Wordlist[%d]=\"%s\"   Gid=%d   Gsize=%d\n",    i, wordlist[i], anagramgroup[i], groupsizeorder[i]);
 //THIS IS WHAT NEEDS CHANGING****
 for (i=0; i<5 && largestfive[i+1]!=0; i++)
    printf("Group size of: %d ", largestfive[i+1]);
    printf("\n");
 

 return 0;


void ReadWord(char word [])

 scanf ("%s", word);


void AssignAnagramGroup (int wordcount)

 int j;
    if (wordcount==0)
    
        anagramgroup[0]=1;
        anagramgroupsize[1]=1;
    
    else
    
        for (j=0; j<wordcount; j++)
        
            if (isAnagram(wordlist[wordcount],  wordlist[j]) ==1)
            
                anagramgroup[wordcount]=anagramgroup[j];
                anagramgroupsize[anagramgroup[j]]+=1;
                break;
            
        

        if (j==wordcount)
                anagramgroup[wordcount]=++anagramgroupcount;
            anagramgroupsize[anagramgroup[j]]+=1;
        
 
    return;
   

int isAnagram (char word1[],char word2[])

   int first[26] = 0, second[26] = 0, c = 0;

   while (word1[c] != '\0')
   
      first[word1[c]-'a']++;
      c++;
   
   c = 0;

   while (word1[c] != '\0')
   
      second[word2[c]-'a']++;
      c++;
   

   for (c = 0; c < 26; c++)
   
      if (first[c] != second[c])
         return 0;
   

   return 1;


void SortWordList()

 int i, j, k, temp;
 char tempchar[21];
    for (i=0; i<wordcount; i++) 
        for (j=0; j<wordcount-1; j++) 
            if (strcmp(wordlist[j], wordlist[j+1])>0)
                strcpy(tempchar, wordlist[j]);
                strcpy(wordlist[j], wordlist[j+1]);
                strcpy(wordlist[j+1], tempchar);

                temp=anagramgroup[j];
                anagramgroup[j]=anagramgroup[j+1];
                anagramgroup[j+1]=temp;
            
        
    

 for (i=0; i<wordcount; i++)
    groupsizeorder[i]=anagramgroupsize[anagramgroup[i]];
 return;


void CalculateTopFiveGroups()

 int i, j, temp;
 int anagramtemp[101]=0;
 for (i=1; i<=anagramgroupcount; i++)
    anagramtemp[i]=anagramgroupsize[i];
 

    for (i=0; i<anagramgroupcount; i++) 
        for (j=0; j<anagramgroupcount-i; j++) 
            if (anagramtemp[j+1]<anagramtemp[j+2])
                temp=anagramtemp[j+1];
                anagramtemp[j+1]=anagramtemp[j+2];
                anagramtemp[j+2]=temp;
            
        
    
 for (i=1; i<=5; i++)
    largestfive[i]=anagramtemp[i];
 
return; 

我目前有输出

ReadWord: WordList[0]="ate" AnagramGroup=1
ReadWord: WordList[1]="eta" AnagramGroup=1
ReadWord: WordList[2]="colli" AnagramGroup=2
ReadWord: WordList[3]="icoll" AnagramGroup=2
ReadWord: WordList[4]="eat" AnagramGroup=1
ReadWord: WordList[5]="lloci" AnagramGroup=2
ReadWord: WordList[6]="cat" AnagramGroup=3
ReadWord: WordList[7]="tac" AnagramGroup=3
WordList size=8 words
Anagram Group 1 of size 3
Anagram Group 2 of size 3
Anagram Group 3 of size 2

WordList[0]="ate" Gid=1 Gsize=3
WordList[1]="cat" Gid=3 Gsize=2
WordList[2]="colli" Gid=2 Gsize=3
WordList[3]="eat" Gid=1 Gsize=3
WordList[4]="eta" Gid=1 Gsize=3
WordList[5]="icoll" Gid=2 Gsize=3
WordList[6]="lloci" Gid=2 Gsize=3
WordList[7]="tac" Gid=3 Gsize=2
Group of size 3
Group of size 3
Group of size 2

这是我需要输出包含的内容

ReadWord: WordList[0]="ate" AnagramGroup=1
ReadWord: WordList[1]="eta" AnagramGroup=1
ReadWord: WordList[2]="colli" AnagramGroup=2
ReadWord: WordList[3]="icoll" AnagramGroup=2
ReadWord: WordList[4]="eat" AnagramGroup=1
ReadWord: WordList[5]="lloci" AnagramGroup=2
ReadWord: WordList[6]="cat" AnagramGroup=3
ReadWord: WordList[7]="tac" AnagramGroup=3
WordList size=8 words
Anagram Group 1 of size 3
Anagram Group 2 of size 3
Anagram Group 3 of size 2

WordList[0]="ate" Gid=1 Gsize=3
WordList[1]="cat" Gid=3 Gsize=2
WordList[2]="colli" Gid=2 Gsize=3
WordList[3]="eat" Gid=1 Gsize=3
WordList[4]="eta" Gid=1 Gsize=3
WordList[5]="icoll" Gid=2 Gsize=3
WordList[6]="lloci" Gid=2 Gsize=3
WordList[7]="tac" Gid=3 Gsize=2
Group of size 3:[GID: 1] ate eat eta
Group of size 3:[GID: 2] colli icoll lloci
Group of size 2:[GID: 3] cat tac

我很确定我需要更改 main 中标记为 //THIS IS WHAT NEEDS CHANGING**** 的最后一个打印件

如果有人不能帮助我,并弄清楚如何使最后的打印语句正确,我将非常感激。

【问题讨论】:

【参考方案1】:

这适用于您的特定示例,但在使用其他示例时会出现一些错误。不过也许会有所帮助。

`do 
    ReadWord(word); 
    if (strcmp(word, "STOP") == 0 || wordcount > 99) // if STOP is entered or 100 words have been read
        break;                   // the input loop will be exited
    strcpy(wordlist[wordcount++], word);         // assigns words read in to the word list
    AssignAnagramGroup(wordcount-1);         // assigns word read in to an anagram group
 while (!done);    // forever loop, only exits on break condition

SortWordList(); // sorts the word list
TopFiveGroups(); // finds the top five largest groups

for (i=0; i<wordcount; i++) 
    printf ("Wordlist[%d] = \"%s\"  AnagramGroup= %d, Gsize=%d\n", i, wordlist[i], anagramgroup[i], groupsizeorder[i]);

printf("Wordlist size = %d words \n", wordcount);

for (i=0; i<5 && largest[i+1] != 0; i++)       // prints other stuff

    printf("Anagram Group %d of size %d ", i+1, largest[i+1]);
    for (j=0; j<wordcount; j++)                 // prints words
        if (anagramgroup[j] == i+1)
            printf("%s ", wordlist[j]);
    printf("\n");

return 0;`

【讨论】:

以上是关于给定一些文本输入,找到最大的字谜组的主要内容,如果未能解决你的问题,请参考以下文章

如何对文本文件进行排序以在 O(MN) 时间复杂度中查找字谜,其中 M 是最大字符数,N 是单词数?

打印输入字谜中字谜和字谜单词本身的最大出现次数

给定一些输入,找到图边的最佳权重以最大化一些输出

在文件中查找给定单词的字谜

找到小于给定整数的最大有趣数字

如何找到具有一组整数的函数的“最大绝对和”