单词接龙~~

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<string> word_list;

void sort_word_list(string end_alpha) 
{
    for (int i = 0; i < word_list.size() - 1; i++) 
    {
        for (int j = i + 1; j < word_list.size(); j++) 
        {
            if (word_list[i].find(end_alpha) == 0) 
            {
                if (word_list[j].find(end_alpha) == 0) 
                {
                    if (word_list[i].length() == word_list[j].length()) 
                    {
                        if (word_list[i] > word_list[j]) 
                        {
                            swap(word_list[i], word_list[j]);
                        }
                    } 
                    else if (word_list[i].length() < word_list[j].length()) 
                    {
                        swap(word_list[i], word_list[j]);
                    }
                } 
            } 
            else 
            {
                if (word_list[j].find(end_alpha) == 0) 
                {
                    swap(word_list[i], word_list[j]);
                }
            }
        }
    }
}

int main() 
{
    int index, num;
    cin >> index >> num;
    cin.ignore();
    for (int i = 0; i < num; i++) 
    {
        string word;
        getline(cin, word);
        word_list.push_back(word);
    }
    string ans = word_list[index];
    string end_alpha = ans.substr(ans.length() - 1, 1);
    word_list.erase(word_list.begin() + index);

    sort_word_list(end_alpha);

    while (true) 
    {
        if (word_list.size() == 0) 
        {
            break;
        }
        if (word_list[0].find(end_alpha) != 0) 
        {
            break;
        }

        ans += word_list[0];
        word_list.erase(word_list.begin());
        end_alpha = ans.substr(ans.length() - 1, 1);
        sort_word_list(end_alpha);
    }

    cout << ans << endl;

    return 0;
}
 

相关推荐

  1. 127. 单词

    2024-03-18 09:48:02       55 阅读
  2. 127. 单词

    2024-03-18 09:48:02       59 阅读
  3. 单词~~

    2024-03-18 09:48:02       37 阅读
  4. 76 BFS解单词

    2024-03-18 09:48:02       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 09:48:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 09:48:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 09:48:02       87 阅读
  4. Python语言-面向对象

    2024-03-18 09:48:02       96 阅读

热门阅读

  1. 51单片机与ARM单片机的区别

    2024-03-18 09:48:02       38 阅读
  2. Spring(创建对象的方式3个)

    2024-03-18 09:48:02       39 阅读
  3. 设计模式--访问者模式(Visitor Pattern)

    2024-03-18 09:48:02       42 阅读
  4. To configure two different databases in Spring Boot

    2024-03-18 09:48:02       38 阅读
  5. odoo中传递上下文

    2024-03-18 09:48:02       44 阅读
  6. React高阶组件详解

    2024-03-18 09:48:02       47 阅读
  7. Flutter 当涉及Listview的复杂滑动布局良好布局方式

    2024-03-18 09:48:02       38 阅读
  8. Python实现连连看

    2024-03-18 09:48:02       42 阅读
  9. 如何优化查询ORM

    2024-03-18 09:48:02       43 阅读
  10. IDEA SpringBoot + Gradle无法运行测试问题

    2024-03-18 09:48:02       40 阅读