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;
}
相关推荐
xier_ran28 分钟前
关键词解释:DAG 系统(Directed Acyclic Graph,有向无环图)
python·算法
CAU界编程小白37 分钟前
数据结构系列之十大排序算法
数据结构·c++·算法·排序算法
好学且牛逼的马1 小时前
【Hot100 | 6 LeetCode 15. 三数之和】
算法
橘颂TA1 小时前
【剑斩OFFER】算法的暴力美学——二分查找
算法·leetcode·面试·职场和发展·c/c++
lkbhua莱克瓦241 小时前
Java基础——常用算法4
java·数据结构·笔记·算法·github·排序算法·快速排序
m0_748248022 小时前
揭开 C++ vector 底层面纱:从三指针模型到手写完整实现
开发语言·c++·算法
七夜zippoe2 小时前
Ascend C流与任务管理实战:构建高效的异步计算管道
服务器·网络·算法
Greedy Alg2 小时前
LeetCode 208. 实现 Trie (前缀树)
算法
Kt&Rs2 小时前
11.5 LeetCode 题目汇总与解题思路
数据结构·算法·leetcode
还是码字踏实2 小时前
基础数据结构之数组的前缀和技巧:和为K的子数组(LeetCode 560 中等题)
算法·leetcode·前缀和·哈希字典