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

int main()
{
	int row;
	string str;
	cout << "Enter number of rows in wordArray: ";
	cin >> row;
	while (cin.get() != '\n')
	{
		continue;
	}
	vector<vector<char>> 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<string> 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<string> wordsHash(words.begin(), words.end()); //哈希表存储单词
	unordered_set<char> 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++;
				}
			}
		}
	}
}

示例:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值