最长字符串 / 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;
}
相关推荐
UIU11413 小时前
P16237 [蓝桥杯 2026 省 B] 应急布线
数据结构·c++·算法·蓝桥杯
爱聊灵光的维C2 天前
C语言初阶:关于函数设计的详细讲解和注意事项
c语言·开发语言·青少年编程·蓝桥杯·学习方法
GG.WinGel5 天前
三元组中心问题
java·数据结构·算法·青少年编程·蓝桥杯
GG.WinGel6 天前
质数数目(蓝桥杯练习题)
java·数据结构·算法·青少年编程·蓝桥杯
Tsuki_tl7 天前
【优选算法 — 双指针】经典例题题解+算法总结
算法·蓝桥杯·力扣·刷题·双指针·1024程序员节·优选算法
十二8 天前
【洛谷题解】P1115 最大子段和(前缀和 + 维护最小前缀)
数据结构·c++·算法·蓝桥杯
青少儿编程课堂11 天前
2026蓝桥杯青少组:校园飞鸟穿越赛解析
蓝桥杯·scratch·少儿编程·图形化编程·蓝桥杯青少组
aqiu11111113 天前
蓝桥杯真题复盘:Fibonacci 数列与黄金分割(C++ 算法详解)
c++·算法·蓝桥杯
aqiu11111113 天前
题目总结:[P8752 [蓝桥杯 2021 省 B2] 特殊年份]
蓝桥杯
青少儿编程课堂14 天前
2026蓝桥杯青少组:校园防疫大作战解析
蓝桥杯·scratch·少儿编程·图形化编程·蓝桥杯青少组