最长字符串 / 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;
}
相关推荐
CoderYanger1 天前
14届蓝桥杯省赛Java A 组Q1~Q3
java·开发语言·线性代数·算法·职场和发展·蓝桥杯
云泽8081 天前
第十五届蓝桥杯大赛软件赛省赛C/C++大学B组
c语言·c++·算法·蓝桥杯
fysuccess1 天前
Ubuntu 22.04 零基础安装 Hermes 完整入门指南
linux·ubuntu·蓝桥杯
j_xxx404_2 天前
C++算法:哈希表(简介|两数之和|判断是否互为字符重排)
数据结构·c++·算法·leetcode·蓝桥杯·力扣·散列表
门左有棵树3 天前
蓝桥杯C++组算法知识点整理(考前急救)
c++·算法·蓝桥杯
wengqidaifeng4 天前
第十七届蓝桥杯C/C++软件赛B组算法题讲解
c语言·c++·蓝桥杯
apcipot_rain4 天前
【天梯赛】2026天梯赛模拟赛——题解
开发语言·c++·算法·蓝桥杯·天梯赛
努力的小帅4 天前
蓝桥杯——入门
c语言·单片机·蓝桥杯
wengqidaifeng4 天前
第十七届蓝桥杯C/C++软件赛C组算法题讲解
c语言·c++·蓝桥杯
j_xxx404_5 天前
力扣题型--链表(两数相加|两两交换链表中的节点|重排链表)
数据结构·c++·算法·leetcode·蓝桥杯·排序算法