最长字符串 / 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;
}
相关推荐
仰泳的熊猫14 小时前
蓝桥杯算法提高VIP-种树
数据结构·c++·算法·蓝桥杯·深度优先·图论
筱昕~呀16 小时前
冲刺蓝桥杯-DFS板块(第一天)
算法·蓝桥杯·深度优先
yzx9910132 天前
蓝桥杯备考智能体:构建高并发、智能化编程竞赛助手的深度实践
职场和发展·蓝桥杯
仰泳的熊猫2 天前
题目1549:蓝桥杯算法提高VIP-盾神与积木游戏
数据结构·c++·算法·蓝桥杯
仰泳的熊猫3 天前
题目1545:蓝桥杯算法提高VIP-现代诗如蚯蚓
数据结构·c++·算法·蓝桥杯
仰泳的熊猫3 天前
题目1535:蓝桥杯算法提高VIP-最小乘积(提高型)
数据结构·c++·算法·蓝桥杯
仰泳的熊猫3 天前
题目1531:蓝桥杯算法提高VIP-数的划分
数据结构·c++·算法·蓝桥杯
List<String> error_P4 天前
蓝桥杯基础知识点:模拟-数位操作类题目
python·算法·蓝桥杯
yzx9910134 天前
蓝桥杯智能体开发:从入门到实战经验分享
职场和发展·蓝桥杯
daxi1504 天前
C语言从入门到进阶——第9讲:函数递归
c语言·开发语言·c++·算法·蓝桥杯