最长字符串 / 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;
}
相关推荐
鑫 源3 天前
VS2019 for mac里用c#开发Mac OS应用 避坑 和 源码
职场和发展·蓝桥杯
aqiu1111115 天前
【蓝桥杯】0仙境诅咒 — BFS/DFS 图论连通性问题(C++ 题解)
蓝桥杯·深度优先·宽度优先
捷瑞电子工坊13 天前
嵌入式蓝桥杯从零点亮第一个LED
stm32·蓝桥杯·cubemx·嵌入式·led·ll·锁存
aqiu11111123 天前
【数位 DP / 记忆化搜索】蓝桥云课 - 光轨区的 AI 数 题解
c++·算法·蓝桥杯·数位dp
aqiu11111125 天前
【并查集 / 图论】蓝桥云课 - 魔法大陆的城市群(求无向图连通块数量)题解
蓝桥杯·图论
aqiu1111111 个月前
【LeetCode 902】最大为 N 的数字组合 - 详细题解与数位组合思路
数据结构·算法·leetcode·蓝桥杯·数位dp
Chester_19991 个月前
CSP202312C.树上搜索
开发语言·数据结构·c++·蓝桥杯·stl
CoderYanger1 个月前
A.每日一题:3622. 判断整除性
java·程序人生·算法·leetcode·面试·职场和发展·蓝桥杯
Chester_19991 个月前
CSP202303C.LDAP
开发语言·c++·蓝桥杯·stl
Chester_19991 个月前
CSP202206C.角色授权
开发语言·数据结构·c++·蓝桥杯