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

输出5个最大的字谜组。如果少于5组,则全部输出。通过减小大小对组进行排序。在字典上按字典最小元素断开关系。对于每个组输出,请打印其大小和成员词。按字典顺序对成员词进行排序,并且仅打印一次相等词。

#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上最后一个标有// //这是需要更改的内容的打印****

如果有人不能帮助我,并弄清楚如何使最后一个打印声明正确无误,我将不胜感激。

最佳答案

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

`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;`

关于c - 给定一些文本输入,找到最大的字谜组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35808492/

10-11 18:42