最长字符串 / 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;
}
相关推荐
依然鸣2 天前
蓝桥杯多校模拟赛 题解
数据结构·c++·经验分享·学习·算法·蓝桥杯
卡兹墨8 天前
iOS从打包到上架详细流程
ios·职场和发展·蓝桥杯
不正经学生9 天前
二叉树递归遍历:先根与后根,递归思维的最佳试炼场
c语言·开发语言·数据结构·算法·蓝桥杯
Ivanqhz1 个月前
NFM(神经因子分解机)
开发语言·javascript·人工智能·算法·蓝桥杯
变量未定义~1 个月前
单调栈、单调队列(模板)、子矩阵(模板)
数据结构·算法·蓝桥杯
智者知已应修善业1 个月前
【P12159蓝桥杯数组翻转】2025-4-24
c语言·c++·经验分享·笔记·算法·蓝桥杯
码云数智-大飞1 个月前
从 OC 平滑迁移 Swift 完整方案
职场和发展·蓝桥杯
嘿黑嘿呦2 个月前
chap 8排序
算法·蓝桥杯·排序算法·软件工程
林森lsjs2 个月前
【日耕一题】5. 青春常数(17届蓝桥杯C++B组第一题)
算法·蓝桥杯
Y_Bk2 个月前
第十七届蓝桥杯C/C++A组省赛
c语言·数据结构·c++·算法·蓝桥杯