最长字符串 / 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;
}
相关推荐
Ivanqhz15 小时前
数据流分析的核心格(Lattice)系统
开发语言·javascript·后端·python·算法·蓝桥杯·rust
仰泳的熊猫18 小时前
题目 1473: 蓝桥杯基础练习VIP-芯片测试
数据结构·c++·算法·蓝桥杯
闻缺陷则喜何志丹2 天前
P12275 [蓝桥杯 2024 国 Python B] 工厂|普及+
c++·算法·蓝桥杯·洛谷
仰泳的熊猫2 天前
题目1465:蓝桥杯基础练习VIP-回形取数
数据结构·c++·算法·蓝桥杯
Better Rose2 天前
【2026蓝桥杯】备赛计划(2个月速成版)
职场和发展·蓝桥杯
llz_1122 天前
蓝桥杯备赛-搜索(DFS/BFS)
c++·算法·蓝桥杯·深度优先·宽度优先
_OP_CHEN2 天前
【算法提高篇】(一)线段树之入门篇:从原理到实战,搞定区间操作难题
数据结构·算法·蓝桥杯·线段树·c/c++·区间查询·acm、icpc
闻缺陷则喜何志丹2 天前
【期望 DFS】P9428 [蓝桥杯 2023 国 B] 逃跑
c++·算法·蓝桥杯·深度优先·洛谷
仰泳的熊猫2 天前
题目1466:蓝桥杯基础练习VIP-字符串对比
数据结构·c++·算法·蓝桥杯
_OP_CHEN3 天前
【算法基础篇】(六十一)SG 函数通关指南:博弈论通用解法,从原理到实战秒杀各类 ICG 游戏
算法·蓝桥杯·c/c++·博弈论·acm/icpc·sg函数·有向图游戏