最长字符串 / 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;
}
相关推荐
养军博客21 小时前
C语言五天速成(可用于蓝桥杯备考 难度中等偏下)
c语言·算法·蓝桥杯
闻缺陷则喜何志丹21 小时前
【栈 递归】P8650 [蓝桥杯 2017 省 A] 正则问题|普及+
c++·数学·蓝桥杯·递归·
仰泳的熊猫1 天前
题目1432:蓝桥杯2013年第四届真题-剪格子
数据结构·c++·算法·蓝桥杯·深度优先·图论
闻缺陷则喜何志丹1 天前
【数论 快速指数幂 龟速乘】P8652 [蓝桥杯 2017 国 C] 小数第 n 位|普及+
c++·蓝桥杯·数论·快速指数幂·龟速乘
谁刺我心2 天前
【蓝桥杯刷题环境】VScode插件算法刷题Competitive Companion
职场和发展·蓝桥杯
代码无bug抓狂人3 天前
C语言之李白打酒(蓝桥杯省B)
c语言·开发语言·蓝桥杯
XH华4 天前
备战蓝桥杯,第六章:C++语言的输入输出(下)
c++·职场和发展·蓝桥杯
季明洵4 天前
两数之和、四数相加II、三数之和、四数之和
java·数据结构·算法·leetcode·蓝桥杯·哈希算法
代码无bug抓狂人4 天前
C语言之宝石组合(蓝桥杯省B)
c语言·开发语言·蓝桥杯
XH华4 天前
备战蓝桥杯,第五章:string字符串
c++·职场和发展·蓝桥杯