最长字符串 / STL+BFS

题目

代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

int main()
{
    map<vector<int>, vector<string>> a;
    set<vector<int>> c;
    vector<int> initial(26, 0);
    c.insert(initial);
    ifstream infile("words.txt");
    string s;

    while (getline(infile, s))
    {
        vector<int> b(26, 0);
        for (size_t j = 0; j < s.size() - 1; j++)
            b[s[j] - 'a']++;
        a[b].push_back(s);
    }

    string e = "~";
    while (!c.empty())
    {
        set<vector<int>> temp_d;
        for (const auto &i : c)
        {
            if (a.find(i) == a.end())
                continue;
            for (const string &j : a[i])
            {
                if (j.size() > e.size())
                    e = j;
                else if (j < e)
                    e = j;
                vector<int> k = i;
                k[j.back() - 'a']++;
                if (a.find(k) != a.end())
                    temp_d.insert(k);
            }
        }
        c = temp_d;
    }
    cout << e;
    return 0;
}
相关推荐
qiuyunoqy16 小时前
蓝桥杯算法之搜索章 - 3
c++·算法·蓝桥杯·深度优先·dfs·剪枝
snowfoootball1 天前
2025 蓝桥杯C/C++国B 部分题解
c语言·c++·笔记·学习·贪心算法·蓝桥杯
爱吃生蚝的于勒3 天前
一文学会c++继承 组合
java·c语言·开发语言·数据结构·c++·算法·蓝桥杯
echo_pen3 天前
蓝桥杯----锁存器、LED、蜂鸣器、继电器、Motor
单片机·蓝桥杯
qiuyunoqy5 天前
蓝桥杯算法之搜索章 - 2
算法·蓝桥杯·深度优先
No0d1es6 天前
第15届蓝桥杯Scratch图形化国赛初/中级组2024年9月7日真题
青少年编程·蓝桥杯·scratch·国赛·图形化
嗨信奥编程6 天前
2025年蓝桥杯青少图形化编程国考真题——摆放玩具
青少年编程·蓝桥杯
echo_pen7 天前
蓝桥杯----串口
单片机·蓝桥杯
echo_pen7 天前
蓝桥杯----超声波
单片机·蓝桥杯
No0d1es7 天前
第12届蓝桥杯Scratch图形化【省赛】初级组 2021年4月24日
青少年编程·蓝桥杯·scratch·省赛