The solution of all the inflection words about a word in programming Zhu Ji Nanxiong

Source: Internet
Author: User

Recently in reading programming Zhu Ji Nanxiong, is really a good book, Ah, for practical programming often encountered problems, guide readers to think, to find the best solution.

Don't say much nonsense. The problem is described as follows:

Given a dictionary of English words, find all the inflection words of a given word. For example, "dog" the word, its conjugation may have "Dgo", "Ogd", "God" and so on. The solution given in the book is to give each word a label. For example, the "dog" bar tag is "D1g1o1", the idea is to count the number of occurrences of each letter. For example, the word "Totto", then its label is "T3o2", so that if two words are inflection words, then their labels are the same.

To summarize, what we need to do is three things.

1. Set the label for all the words in the dictionary.

2. Sort the words in the dictionary according to their labels.

3. Find the modified word for the specified word

The code is as follows: The code uses C + +, the compilation environment is VS2010, may have many in the code to use STL knowledge, can not understand the self-brain complements.

Problem description, given a dictionary and a word, find out the variable shape of all the words in the dictionary. If a dog appears in the dictionary, DGO,ODG, and so on, matches the search criteria//1. Sets the signature for each word of a string in the dictionary, and the signature of the string with the same shape is the same//2.
Sorts all strings according to the signature dictionary, sorted by the string itself, in the same case as the signature. The problem comes from programming Zhu Ji Nanxiong the first question in Chapter two. Find efficiency must be concerned.
Schedule two hours to complete .... YOGHURT2014/7/26 Wuhan University///It took one hours to experiment with the variable-body index of individual words.
In addition to a simple loop, you can implement all the strings in the array//tomorrow to finish sorting by the variable index.
Familiarize yourself with the use of the sort algorithm under list.                                  
#include <iostream> #include <algorithm> #include <vector> #include <list>
#include <string> #include <iterator> using namespace std;
const int maxamount = 10000;								struct directionary{//defines a struct-body string word;							The word itself string wordindex;

The word variable body number};                       Directionary findstring; Need to find the word, here for the convenience of the program, I used the struct bool Sort_by_wordindex (const directionary *d1,const directionary *d2)//Sort by the word body number {RET
Urn D1->wordindex < d2->wordindex; } bool Findwordindex (const directionary *d)//Find the word with the same word in the dictionary variable form {return D->wordindex = = Findstring.woRdindex; } void Computewordindex (Directionary &s)//Calculate the variable body number of a word, such as "dog", then change form to "d1g1o1" {int arr                                            AY26[26] = {0}; The number of letters appearing in each word is initialized to 0 for (int i = 0; i < s.word.length (); ++i) {//Count occurrences of each letter in a word char c = s.wo
		Rd[i];
	Array26[int (c ' a ')] + +;
	} int num = 0;
	for (int i = 0; i < 26;++i) {//statistics not the same as the number of letters if (Array26[i]) num++;                                        } s.wordindex.resize (num*2);
	Open storage space int n = 0 for conjugation words;
			for (int i = 0;i < 26;++i) {if (Array26[i]) {//cout << char (i+ ' a ');                          s.wordindex[n++] = char (i+ ' a ');
			Copy the alphabetic characters to the//cout << Array26[i];                  s.wordindex[n++] = char (array26[i]+48);            To copy a numeric character to a modified Word}}} int main () {int array26[26] = {0}; Represents the number of occurrences in a string, such as Dogg, then directionary *d = new Directionary[maxamount];            In the initialized dictionary, there are 10,000 words, which is almost the average dictionary only so much d[0].word = "dog";
	D[1].word = "ODG";
	D[2].word = "God";
	D[3].word = "OGD";
	for (int i = 4;i < Maxamount;++i) {D[i].word = "Jiajia";
	} D[800].word = "Dgo";
	Put the elements into the list.                                       list<directionary*>l;
	Here I choose the container list, OK, I think the best set, or map container, can implement the logarithmic lookup for (int i = 0;i < Maxamount;++i) {L.push_back (&d[i]);

	} List<directionary*>::iterator Liteator;
		for (Liteator = L.begin (), Liteator!=l.end (); ++liteator) {for (int k = 0;k < 26;++k) {array26[k] = 0;
			} for (int i = 0; i < (*liteator)->word.length (); ++i) {char c = (*liteator)->word[i];
		Array26[int (c ' a ')] + +;
		} int num = 0;
		for (int i = 0; i < 26;++i) {if (array26[i]) num++;
	
		} (*liteator)->wordindex.resize (num*2);
		int n = 0;
				for (int i = 0;i < 26;++i) {if (Array26[i]) {//cout << char (i+ ' a ');
				(*liteator)->wordindex[n++] = char (i+ ' a '); cout <&Lt
				Array26[i];
			(*liteator)->wordindex[n++] = char (array26[i]+48);
	}}}///Last step to find all deformed words for a single input word.
	cout << "Input the string want to find:";
	Cin >> Findstring.word;
	Calculates the shape of the word.
	Computewordindex (findstring);
	Liteator = find_if (L.begin (), L.end (), findwordindex);
			while (Liteator!=l.end ()) {if (*liteator)->word! = Findstring.word) {//synonyms should not include themselves ....
		cout << "and" << Findstring.word << "words that belong to synonyms are:" << (*liteator)->word << Endl;                   } liteator = Find_if (++liteator,l.end (), findwordindex);
		
	Proceed to the next round of iterations.
	} Delete []d;
return 0; }

The code should use the set or map container, because these containers have their own find function, can achieve logarithmic time lookup, more efficient.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.