UVA10391 Compound Words 复合词 解题报告

UVA10391 Compound Words 复合词 解题报告

题目链接

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

题目大意

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

解题思路

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

代码

#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. UVA10391 Compound Words 复合词 解题报告

    2024-04-09 17:48:02       29 阅读
  2. Compound Words(UVA 10391

    2024-04-09 17:48:02       49 阅读
  3. UVA1595 Symmetry 对称轴 解题报告

    2024-04-09 17:48:02       33 阅读
  4. UVA247 Calling Circles 解题报告

    2024-04-09 17:48:02       34 阅读
  5. UVA10935 Throwing cards away I 卡片游戏 解题报告

    2024-04-09 17:48:02       31 阅读
  6. UVA839 Not so Mobile 天平 解题报告

    2024-04-09 17:48:02       28 阅读
  7. UVA230 Borrowers 图书管理系统 解题报告

    2024-04-09 17:48:02       39 阅读
  8. 洛谷P10397题解

    2024-04-09 17:48:02       30 阅读
  9. LeetCode --- 2103. Rings and Rods 解题报告

    2024-04-09 17:48:02       20 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-09 17:48:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-09 17:48:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-09 17:48:02       87 阅读
  4. Python语言-面向对象

    2024-04-09 17:48:02       96 阅读

热门阅读

  1. Python入门:轻松学习,编程不再难

    2024-04-09 17:48:02       36 阅读
  2. 云智前端面试题

    2024-04-09 17:48:02       32 阅读
  3. 服务器硬件基础知识解析

    2024-04-09 17:48:02       31 阅读
  4. Vue.nextTick() 使用场景及实现原理

    2024-04-09 17:48:02       38 阅读
  5. DockerFile定制镜像

    2024-04-09 17:48:02       32 阅读
  6. Vue3 · 小白学习全局 API:常规

    2024-04-09 17:48:02       38 阅读
  7. 面试前必看,仅供参考

    2024-04-09 17:48:02       34 阅读