Problem Description:
C. Given an English dictionary, find out the set of all the conjugation words. For example, "pots", "Stop", and "tops" are mutually modified words, because each word can be obtained by changing the order of the letters in other words.
Problem Resolution:
1, the inflection word has the same length, the same character, the only difference is that the same characters are arranged in different order into different strings. If there is a way to uniquely identify these same characters, then this problem is solved.
Solution : Scenario 1: Each word is identified in alphabetical order and the words with the same identity are assembled together.
(1) Identify and output all the words in the input file to another file. The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
|
#include <cstdio> #include <cstdlib>//Qsort #include <cctype>//Toupper\tolower #include <cstring>//strlen #include <cassert>//Assert #define WORDMAX 100 #define ERROR (STR) fatal_error (str) #define FATAL_ERROR (str) fprintf (stderr, "%s\n", str), exit (1) int Charcomp (const void* x, const void* y) {return * (char*) x-* (char*) y;} /************************************************************************/ Function Name: Mytolower function Purpose: To convert uppercase characters in a string to lowercase characters Function arguments: Lword: Converted string, Word: string to convert function return: converted String Conditions of Use: /************************************************************************/ char* Mytolower (char* Lword, char* Word) { while (*word! = ')} { if (Isalpha (*word) && isupper (*word)) {*lword++ = ToLower (*word++);} else {*lword++ = *word++;} } *lword = ' + '; End Plus End character return Lword; } /************************************************************************/ Function Name: add_sign function Purpose: Get the word ID and output it to a file Function parameters: Rfile: To read the file, Wfile: The file to be written function return: None Conditions of use: the input word length is less than 100 /************************************************************************/ void Add_sign (file* rfile, file* wfile) { ASSERT (Rfile = null && wfile! = null); Char Word[wordmax], Lword[wordmax], Sign[wordmax]; while (FSCANF (Rfile, "%s", word)! = EOF) { Mytolower (Lword, Word); strcpy (sign, Lword); Qsort (sign, strlen, sizeof (char), charcomp); fprintf (Wfile, "%s\t%s\r\n", sign, word); } Return } int main () { file* rfile = fopen ("Dictionary.txt", "R"); if (NULL = = rfile) {fatal_error ("Cannot open the Dictionary.txt file. \ n "); } file* wfile = fopen ("Sign_dictionary.txt", "w"); if (NULL = = wfile) {fatal_error ("Cannot open the Sign_dictionary.txt file. \ n "); } Add_sign (Rfile, wfile);
Fclose (Rfile); Fclose (Wfile); printf ("The build is complete. "); return 0; } |
Simple test data dictionary.txt and generated data sign_dictionary.txt see:
http://download.csdn.net/detail/johnnyhu90/8346745
(2) store all the words in the identified output file in memory in the form of (identifier, word) pairs. This is done using C + + 's Mutimap and set, with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
|
#include <iostream> #include <map> #include <set> #include <string> using namespace Std; /************************************************************************/ Function Name: Print_anagrams function Purpose: Output print all the inflection words function parameter: rfile: to read the file function return: None Conditions of Use: Rfile is a file with (identification, conjugation) pairs /************************************************************************/ void Print_anagrams (file* rfile) { Char Word[wordmax], Sign[wordmax]; Multimap<string,string> Angrams; Std::set<string> MySet; while (FSCANF (Rfile, "%s\t%s", sign, word)! = EOF) { Myset.insert (sign); Angrams.insert (Std::make_pair (sign, word)); } for (Set<string>::iterator iter = Myset.begin (); ITER! = Myset.end (); ++iter) { Multimap<string, String>::iterator it = Angrams.equal_range (*iter). for (; it! = Angrams.equal_range (*iter). Second; ++it) { Std::cout << ' << (*it). Second; } cout << Endl; } Return } int main () { file* rfile = fopen ("Sign_dictionary.txt", "R"); if (NULL = = rfile) {fatal_error ("Cannot open the Sign_dictionary.txt file. \ n "); } Print_anagrams (Rfile);
Fclose (Rfile); printf ("Execution completed: "); return 0; } |
The output results are as follows:
experience and doubts:
1. When we identify all the words and store the identifiers and their corresponding words in a file on the hard disk, how to sort the file by identity (assuming that the memory is not able to load the data all at once).