c++学习笔记(34) map的应用,及字典差一字母算法的学习

1. 首先参考(http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html)了解下map的一些应用函数的应用。

2. 利用上述的函数自己写了写,了解了一些。

#include "map"
#include "cstring"
#include "string"
#include "iostream"
#include "vector"
using namespace std;

int main()
{
	//map 类型创建
	map<int, string> m1;
	string _map[3] = {"hello", "hai", "nihao"};

	//map 的插入
	for(int i = 0; i < 3; i ++)
	{
		m1[i] = _map[i];
	}
	m1.insert(pair<int, string>(10, "xiaomi"));
	m1.insert(pair<int, string>(6, "nuojiya"));
	
	map<int, string>::iterator m1_iter;
	for(m1_iter = m1.begin(); m1_iter != m1.end(); m1_iter++)
	{
		cout << m1_iter->second << " ";
	}
	cout << "." << endl;

	//查找
	m1_iter = m1.find(3);  //查找键值
	if(m1_iter == m1.end())
		cout << "not find" << endl;
	else 
		cout << "OK!" << endl;

	//交换两个容器
	cout << "the size of m1: " << m1.size() << endl; 
	map<int, string> m2;
	m1.swap(m2);
	cout << m1.size() << endl;
	cout << m2.size() << endl;

	cin.get();
	cin.get();
	return 0;
}



3.参考《数据结构与算法分析c++》中字典差一字母查找算法

//《数据结构与算法分析c++》中字典词替换查找方法
//map 打印函数,将字典进行打印
void printHighChangeables(const map<string, vector<string>> &adjwords, int minWords = 15)
{
	map<string, vector<string>>::const_iterator itr;

	for(itr = adjwords.begin(); itr != adjwords.end(); itr++)
	{
		const pair<string, vector<string>> &entry = *itr;
		const vector<string>               &words = entry.second;
		
		if(words.size() >= minWords)
		{
			cout << entry.first << " (" << words.size() << "):";
			for(int i = 0; i < words.size(); i++)
				cout << " " << words[i];
			cout << endl;
		}
	}
}

//判断是否只是有一个字母不同的单词
//Returns true if word1 and word2 are the same length
//and differ in only one character.
bool oneCharoff(const string &word1, const string &word2)
{
	if(word1.length() != word2.length())
		return false;

	int diffs = 0;
	
	for(int i = 0; i < word1.length(); i++)
		if(word1[i] != word2[i])
			diffs++;
	if(diffs > 1)
		return false;

	return diffs == 1;
}

//强行搜索
//computes a map in which the keys are word and values are vectors of words 
//that differ in only one character from the corresponding key.
//Uses a quadratic algorithm
map<string, vector<string>> computeAdjacentWords(const vector<string> &words)
{
	map<string, vector<string>> adjWords;
	
	for(int i = 0; i < words.size(); i++)
		for(int j = i+1; j < words.size(); j++)
		{
			if(oneCharoff(words[i], words[j]))
			{
				adjWords[words[i]].push_back(words[j]);
				adjWords[words[j]].push_back(words[i]);
			}
		}

	return adjWords;
}

//首先进行分组,根据字母的个数进行分组,然后再进行搜索
//uses a quadratic algorithm, but speeds thing up a little by 
//maintaing an additional map that group words by their length.
map<string, vector<string>> computeAdjacentWords(const vector<string> &words)
{
	map<string, vector<string>> adjWords;
	map<int, vector<string>> wordsByLength;

	//group the words by their length
	for(int i = 0; i < words.size(); i++)
		wordsByLength[words[i].length()].push_back(words[i]);

	//work on each group separately
	map<int, vector<string>>::const_iterator itr;
	for(itr = wordsByLength.begin(); itr != wordsByLength.end(); itr++)
	{
		const vector<string> & groupsWords = itr->second;
		
		for(int i = 0; i < groupsWords.size(); i++)
			for(int j = i+1; j < groupsWords.size(); j++)
			{
				if(oneCharoff(groupsWords[i], groupsWords[j]))
				{
					adjWords[groupsWords[i]].push_back(groupsWords[j]);
					adjWords[groupsWords[j]].push_back(groupsWords[i]);
				}
			}
	}

	return adjWords;
}


//每个字母每个字母的遍历,首先第一个字母不同,进行寻找,然后第二,第三...
//uses an efficient algorithm that is o(NlogN) with a map
map<string, vector<string>> computeAdjacentWords(const vector<string> &words)
{
	map<string, vector<string>> adjWords;
	map<int, vector<string>> wordsByLength;

	//group the words by their length
	for(int i = 0; i < words.size(); i++)
		wordsByLength[words[i].length()].push_back(words[i]);

	//work on each group separately
	map<int, vector<string>>::const_iterator itr;
	for(itr = wordsByLength.begin(); itr != wordsByLength.end(); itr++)
	{
		const vector<string> & groupsWords = itr->second;  //一组单词中的单词
		int groupNum = itr->first;
		
		//work on each position in each group
		for(int i = 0; i < groupNum; i++)   //第几组
		{
			//Remove a character in given position, computing representative.
			//words with same representatives are adjacent; so populate a map
			map<string, vector<string>> repToWord;

			for(int j = 0; j < groupsWords.size(); j++)   //从这个组中的每个单词进行
			{
				string rep = groupsWords[j];   //从组中的单词逐个进行删除
				rep.erase(i, 1);   //一个字母一个字母的删除
				repToWord[rep].push_back(groupsWords[j]);   //将所有的在其中一组的单词每个的相应字母都去掉,然后进行遍历查找.相当于已经分好了类了
			}

			//and then look for map value with more than one string
			map<string, vector<string>>::const_iterator itr2;
			for(itr2 = repToWord.begin(); itr2 != repToWord.end(); itr2++)
			{
				const vector<string> &clique = itr2->second;
				if(clique.size() >= 2)
					for(int p = 0; p < clique.size(); p++)
						for(int q = p + 1; q < clique.size(); q++)
						{
							adjWords[clique[p]].push_back(clique[q]);
							adjWords[clique[q]].push_back(clique[p]);
						}
			}
		}
	}

	return adjWords;
}
























  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值