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;
}
相关推荐
励志要当大牛的小白菜11 分钟前
ART配对软件使用
开发语言·c++·qt·算法
qq_5139704414 分钟前
力扣 hot100 Day56
算法·leetcode
PAK向日葵1 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
爱喝矿泉水的猛男3 小时前
非定长滑动窗口(持续更新)
算法·leetcode·职场和发展
YuTaoShao3 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先
YouQian7724 小时前
Traffic Lights set的使用
算法
go54631584655 小时前
基于深度学习的食管癌右喉返神经旁淋巴结预测系统研究
图像处理·人工智能·深度学习·神经网络·算法
aramae5 小时前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法
大锦终6 小时前
【算法】前缀和经典例题
算法·leetcode
想变成树袋熊6 小时前
【自用】NLP算法面经(6)
人工智能·算法·自然语言处理