栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构与算法分析C++习题5.18答案

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构与算法分析C++习题5.18答案

int main()
{
	int row;
	string str;
	cout << "Enter number of rows in wordArray: ";
	cin >> row;
	while (cin.get() != 'n')
	{
		continue;
	}
	vector> wordArray(row); //输入字谜表
	cout << "Enter elements of wordArray:n";
	for (int i = 0; i < row; i++)
	{
		getline(cin, str);
		for (int j = 0; j < str.length(); j++)
		{
			if (isalpha(str[j]))
			{
				wordArray[i].push_back(str[j]);
			}
		}
	}
	int r = wordArray.size(), c = wordArray[0].size();
	cout << endl;
	cout << "Enter a list of words(q to quit):n"; //输入要从字谜表中查找的单词
	vector words;
	int maxWordSize = 0; //记录单词的最大长度
	cin >> str;
	while (str != "q")
	{
		if (str.size() > maxWordSize)
		{
			maxWordSize = str.size();
		}
		words.push_back(str);
		cin >> str;
	}
	cout << endl;
	cout << "Word position:" << endl;
	unordered_set wordsHash(words.begin(), words.end()); //哈希表存储单词
	unordered_set wordPrefix; //哈希表存储单词的第一个字母
	for (int i = 0; i < words.size(); i++)
	{
		wordPrefix.insert(words[i][0]);
	}
	string temp;
	int k;
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
		{
			if (wordPrefix.count(wordArray[i][j])) //当单词的第一个字母包含wordArray[i][j]时查找从(i,j)开始的八个方向是否有匹配的单词。
			{
				temp = "";
				k = 0;
				while (i + k < r && k < maxWordSize)
				{
					temp += wordArray[i + k][j];
					if (wordsHash.count(temp))
					{
						cout << temp << ": " << "from " << "(" << i << "," << j << ") " << " to "
							<< "(" << i + k << "," << j << ") " << endl;
					}
					k++;
				}


				temp = "";
				k = 0;
				while (i - k >= 0 && k < maxWordSize)
				{
					temp += wordArray[i - k][j];
					
					if (wordsHash.count(temp))
					{
						cout << temp << ": " << "from " << "(" << i << "," << j << ") " << " to "
							<< "(" << i - k << "," << j << ") " << endl;
					}
					k++;
				}

				temp = "";
				k = 0;
				while (j + k < c && k < maxWordSize)
				{
					temp += wordArray[i][j + k];
				
					if (wordsHash.count(temp))
					{
						cout << temp << ": " << "from " << "(" << i << "," << j << ") " << " to "
							<< "(" << i << "," << j + k << ") " << endl;
					}
					k++;
				}

				temp = "";
				k = 0;
				while (j - k >= 0 && k < maxWordSize)
				{
					temp += wordArray[i][j - k];
					
					if (wordsHash.count(temp))
					{
						cout << temp << ": " << "from " << "(" << i << "," << j << ") " << " to "
							<< "(" << i << "," << j - k << ") " << endl;
					}
					k++;
				}

				temp = "";
				k = 0;
				while (i + k < r && j + k < c && k < maxWordSize)
				{
					temp += wordArray[i + k][j + k];
				
					if (wordsHash.count(temp))
					{
						cout << temp << ": " << "from " << "(" << i << "," << j << ") " << " to "
							<< "(" << i + k << "," << j + k << ") " << endl;
					}
					k++;
				}

				temp = "";
				k = 0;
				while (i + k < r && j - k >= 0 && k < maxWordSize)
				{
					temp += wordArray[i + k][j - k];
				
					if (wordsHash.count(temp))
					{
						cout << temp << ": " << "from " << "(" << i << "," << j << ") " << " to "
							<< "(" << i + k << "," << j - k << ") " << endl;
					}
					k++;
				}

				temp = "";
				k = 0;
				while (i - k >= 0 && j - k >= 0 && k < maxWordSize)
				{
					temp += wordArray[i - k][j - k];
					
					if (wordsHash.count(temp))
					{
						cout << temp << ": " << "from " << "(" << i << "," << j << ") " << " to "
							<< "(" << i - k << "," << j - k << ") " << endl;
					}
					k++;
				}

				temp = "";
				k = 0;
				while (i - k >= 0 && j + k < c && k < maxWordSize)
				{
					temp += wordArray[i - k][j + k];
				
					if (wordsHash.count(temp))
					{
						cout << temp << ": " << "from " << "(" << i << "," << j << ") " << " to "
							<< "(" << i - k << "," << j + k << ") " << endl;
					}
					k++;
				}
			}
		}
	}
}

示例:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/954406.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号