题目描述 单词接龙的规则是:可用于接龙的单词首字母必须要前一个单词的尾字母相同;当存在多个首字母相同的单词时,取长度最长的单词,如果长度也相等,则取字典序最小的单词;已经参与接龙的单词不能重复使用。现给定一组全部由小写字母组成单词数组,并指定其中的一个单词作为起始单词,进行单词接龙,请输出最长的单词串,单词串是单词拼接而成,中间没有空格。输入描述输入的第一行为一个非负整数,表示起始单词在数组中的索引K,0 <= K < N ;输入的第二行为一个非负整数,表示单词的个数N;接下来的N行,分别表示单词数组中的单词。输出描述输出一个字符串,表示最终拼接的单词串。补充说明单词个数N的取值范围为[1, 20];单个单词的长度的取值范围为[1, 30];用例1输入06worddddadcdwordd输出worddwordda说明:先确定起始单词word,再接以d开头的且长度最长的单词dword,剩余以d开头且长度最长的有dd,da,dc,则取字典序最小的da,所以最后输出worddwordda。用例2输入46worddddadcdwordd输出dwordda说明:先确定起始单词word,剩余以d开头且长度最长的有dd,da,dc,则取字典序最小的da,所以最后输出dwordda。解题如下:#include <iostream>#include <vector>#include <string>using namespace std;// 判断是否可以接龙bool canChain(const string&amp; a, const string&amp; b) {return a.back() == b.front();}int main() {int k, n;cin >> k >> n;vector<string> words(n); // 存储单词列表vector<bool> used(n, false); // 标记单词是否已经使用过for (int i = 0; i < n; ++i) {cin >> words[i];}string result = words[k]; // 结果字符串以起始单词开始used[k] = true; // 标记起始单词已使用while (true) {int nextIndex = -1;for (int i = 0; i < n; ++i) {// 找到未使用的单词并且可以接龙if (!used[i] &amp;&amp; canChain(result, words[i])) {// 如果没有选择单词或者当前单词比选择的单词更长或者相同长度但字典序更小if (nextIndex == -1 ||words[i].length() > words[nextIndex].length() ||(words[i].length() == words[nextIndex].length() &amp;&amp; words[i] < words[nextIndex])) {nextIndex = i;}}}// 如果没有找到可以接龙的单词,跳出循环if (nextIndex == -1) {break;}result += words[nextIndex]; // 将选择的单词加入结果used[nextIndex] = true; // 标记该单词为已使用}// 输出最终拼接的单词串cout << result << endl;return 0;}