UVA10391 Compound Words 复合词 解题报告

UVA10391 Compound Words 复合词 解题报告

题目链接

https://vjudge.net/problem/UVA-10391

题目大意

给出一个词典,找出所有的复合词,即恰好有两个单词连接而成的单词。输入每行都是一个由小写字母组成的单词。输入已按照字典序从小到大排序,且不超过120000个单词。输出所有复合词,按照字典序从小到大排列。

解题思路

因为涉及查找效率,所以我们使用set对字符串进行存储,然后我们遍历set中的所有字符串,对于每个字符串s,我们枚举分割位置j,使用substr()方法将字符串s分割为左右两个子串,判断左右两个子串是不是都在set中即可。

代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define endl '\n';
const int maxn = 1e3 + 10;
const int INF = 0x3fffffff;
const int mod = 1e9 + 7;

void solve() {
    set<string> words;
    string str;
    while (cin >> str)
        words.insert(str);
    for (const auto& s : words) {
        for (int j = 1; j < s.size(); j++) {
            string left = s.substr(0, j);
            if (words.count(left)) {
                string right = s.substr(j);
                if (words.count(right)) {
                    cout << s << endl;
                    break;
                }
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(18);
    
    solve();
    return 0;
}
相关推荐
r0ysue_35 分钟前
02.上帝之心算法用GPU计算提速50倍
算法·gpu
L_cl36 分钟前
【Python 算法零基础 4.排序 ⑦ 桶排序】
数据结构·算法·排序算法
小O的算法实验室2 小时前
2025年AIR SCI1区TOP,多策略增强蜣螂算法MDBO+实际工程问题,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
花自向阳开10242 小时前
LeetCode hot100-11
数据结构·算法·leetcode
月亮被咬碎成星星2 小时前
LeetCode[404]左叶子之和
算法·leetcode
有梦想的骇客2 小时前
书籍在其他数都出现k次的数组中找到只出现一次的数(7)0603
算法
jiet_h3 小时前
Android Kotlin 算法详解:链表相关
android·算法·kotlin
数据潜水员4 小时前
C#基础语法
java·jvm·算法
鸽子炖汤4 小时前
LRC and VIP
c++·算法·图论
鑫鑫向栄4 小时前
[蓝桥杯]机器人塔
数据结构·c++·算法·蓝桥杯