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;
}
相关推荐
心中有国也有家1 小时前
cann-recipes-infer:昇腾 NPU 推理的“菜谱集合”
经验分享·笔记·学习·算法
绝知此事1 小时前
【算法突围 01】线性结构与哈希表:后端开发的收纳术
java·数据结构·算法·面试·jdk·散列表
碧海银沙音频科技研究院1 小时前
通话AEC与语音识别AEC的软硬回采链路
深度学习·算法·语音识别
csdn_aspnet2 小时前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
m0_629494735 小时前
LeetCode 热题 100-----26.环形链表 II
数据结构·算法·leetcode·链表
壹号用户5 小时前
用队列实现栈
数据结构·算法
做人求其滴5 小时前
面试经典 150 题 380 274
c++·算法·面试·职场和发展·力扣
daad7775 小时前
记一组无人机IMU传感器数据
算法
计算机安禾5 小时前
【c++面向对象编程】第42篇:模板特化与偏特化:为特定类型定制实现
开发语言·c++·算法