最长字符串 / 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;
}
相关推荐
unicrom_深圳市由你创科技1 天前
Unity开发如何解决iOS闪退问题
unity·ios·蓝桥杯
Thomas_YXQ2 天前
Unity3D游戏内存优化指南
游戏·unity·职场和发展·性能优化·蓝桥杯·游戏引擎·unity3d
minos.cpp16 天前
从厨房到代码台:用做菜思维理解iOS开发 - Swift入门篇①
ios·蓝桥杯·swift
岁忧16 天前
第十六届蓝桥杯C/C++程序设计研究生组国赛 国二
c语言·c++·算法·蓝桥杯
嗨信奥编程18 天前
2023年蓝桥杯青少第十四届蓝桥杯Scratch省赛中级组真题——小狗避障
青少年编程·蓝桥杯
Yingye Zhu(HPXXZYY)19 天前
P12894 [蓝桥杯 2025 国 Java B] 智能交通信号灯
c++·蓝桥杯
MoRanzhi120320 天前
103. 2017年蓝桥杯省赛 - 日期问题(困难)- 暴力枚举
python·蓝桥杯·枚举算法·时间格式转换·暴力搜索·日期处理·2017省赛
jinmo_C++23 天前
第十六届蓝桥杯_省赛B组(C).可分解的正整数
c语言·职场和发展·蓝桥杯
骑驴看星星a25 天前
P10987 [蓝桥杯 2023 国 Python A] 火车运输
职场和发展·蓝桥杯