最长字符串 / 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;
}
相关推荐
MoRanzhi12031 天前
106.正则问题(分治)
python·算法·正则表达式·蓝桥杯·递归·分治算法·字符串处理
MoRanzhi12031 天前
122.四平方和(暴力枚举)
python·数学·算法·蓝桥杯·完全平方数·暴力枚举·四平方和定理
aqiu1111115 天前
蓝桥杯省一学习计划:每天 2 小时,12 周高效备赛
学习·职场和发展·蓝桥杯
智者知已应修善业10 天前
【51单片机只有一个8*8点阵数据如何滚动起来一片595+一组IO+花样】2022-1-15
c++·经验分享·笔记·单片机·蓝桥杯·硬件架构
Dream it possible!13 天前
蓝桥杯_翻转_C++
c++·算法·蓝桥杯
鑫 源18 天前
VS2019 for mac里用c#开发Mac OS应用 避坑 和 源码
职场和发展·蓝桥杯
aqiu11111119 天前
【蓝桥杯】0仙境诅咒 — BFS/DFS 图论连通性问题(C++ 题解)
蓝桥杯·深度优先·宽度优先
捷瑞电子工坊1 个月前
嵌入式蓝桥杯从零点亮第一个LED
stm32·蓝桥杯·cubemx·嵌入式·led·ll·锁存
aqiu1111111 个月前
【数位 DP / 记忆化搜索】蓝桥云课 - 光轨区的 AI 数 题解
c++·算法·蓝桥杯·数位dp
aqiu1111111 个月前
【并查集 / 图论】蓝桥云课 - 魔法大陆的城市群(求无向图连通块数量)题解
蓝桥杯·图论