最长字符串 / 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;
}
相关推荐
汉克老师5 小时前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(1、求和)
c++·蓝桥杯·蓝桥杯c++·c++蓝桥杯
汉克老师1 天前
第十四届蓝桥杯青少组C++国赛[2023.5.28]第二部分编程题(4、 数独填数)
c++·蓝桥杯·蓝桥杯c++·c++蓝桥杯
闻缺陷则喜何志丹1 天前
【 线段树】P12347 [蓝桥杯 2025 省 A 第二场] 栈与乘积|普及+
数据结构·c++·蓝桥杯·线段树·洛谷
古译汉书2 天前
蓝桥杯算法之基础知识(6)
数据结构·算法·蓝桥杯
古译汉书3 天前
蓝桥杯算法之基础知识(4)
开发语言·python·算法·蓝桥杯
古译汉书3 天前
蓝桥杯算法之基础知识(5)
数据结构·算法·蓝桥杯
范纹杉想快点毕业3 天前
数据结构与算法个人学习代码笔记包含leetcode,海贼oj,蓝桥杯,ACM
java·开发语言·笔记·学习·算法·leetcode·蓝桥杯
sssvangen3 天前
拔河(蓝桥杯)(前缀和)
算法·前缀和·职场和发展·蓝桥杯
楼田莉子5 天前
C++算法学习专题:前缀和
c++·学习·算法·leetcode·蓝桥杯
闻缺陷则喜何志丹7 天前
【分治法 BFS 质因数分解】P12255 [蓝桥杯 2024 国 Java B] 园丁|普及+
c++·算法·蓝桥杯·宽度优先·质因数分解·分治法