UVA 10391 Compound Words

本文介绍了一种算法,用于从字典中找出所有由两个单词组成的复合词。算法使用set存储单词,并通过遍历检查每个单词是否能被拆分为两个存在于set中的单词。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

You are to find all the two-word compound words in a dictionary. A two-word compound word is a
word in the dictionary that is the concatenation of exactly two other words in the dictionary.
 

 

Input

Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will
be no more than 120,000 words.
 

 

Output

Your output should contain all the compound words, one per line, in alphabetical order.
 

 

Sample Input

a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra
 

 

Sample Output

alien
newborn

 

 

 

解题思路:将单词保存在set中,遍历set中的元素,将其中的每一个单词拆分成两个单词,对于拆分出来的单词看是否在set中出现过,若两个都出现过,那么该单词为复合词

 

遇到的问题:最开始我用vector来存放string,但是结果为TLE,当把容器换为set以后就AC了。猜想原因有二:一、OJ上测试时输入了重复的数据,因此采用set的效率更高。但是实际上用set程序运行时间为70ms,而vector运行时间大于3000ms,时间差距过大,因此可能不是重复输入数据的原因。二、在用find的时候,vector用的是stl库中的函数,调用形式为find(dic.begin(),dic.end(),temp);而用set的时候使用的set的函数,调用形式为dic.find(temp),因此猜想可能是前者的效率低,直接导致了TLE,该猜想未经验证

 

 

#include<iostream>  
#include<cstdio>  
#include<stack>  
#include<string>
#include<vector>
#include<sstream>
#include<math.h>
#include<map>
#include<string>
#include<set>
#include <algorithm>
using namespace std;


int main() {
	
	string word;
	string temp;
	string remain;
	string a;
	set<string> dic;
	set<string>::iterator it;


	while (cin>>word)
	{			
		dic.insert(word);
	}

	for (it = dic.begin(); it != dic.end(); it++)
	{
		for (int j = 1; j < (*it).size()+1; j++)
		{
			temp = (*it).substr(0, j);
			remain = (*it).substr(j, ((*it)).size()-j);
			set<string>::iterator result1 = dic.find(temp);
			set<string>::iterator result2 = dic.find(remain);
			if (result1 != dic.end()&& result2 != dic.end() )
			{
				cout << (*it) << endl;
				break;
			}
		}
	}
	return 0;
}

 

<think>我们正在讨论Tkinter中的Button组件的compound参数。用户想了解这个参数的用法和含义。在Tkinter中,Button可以同时显示文本和图像。compound参数用于控制文本和图像的相对位置关系。compound参数可以取以下值:-"top":图像在文本上方-"bottom":图像在文本下方-"left":图像在文本左边-"right":图像在文本右边-"center":文本显示在图像上(重叠)另外,如果compound参数不设置(默认情况),那么Button只会显示图像(如果指定了图像的话),文本会被忽略,除非没有指定图像才显示文本。下面是一个使用compound参数的示例代码:```pythonimporttkinterastkroot=tk.Tk()#创建一个图像,这里使用一个内置的信息图标info_icon=tk.PhotoImage(data="R0lGODlhEAAQAPIAAAAAAJmZmWxsbKysrAAAAAAAAAAAAAAAAAAAACH5BAEAAAUALAAAAAAQABAAAAOPWLrc/jDKSWpEIorB5nE1xFIIiIeXqKJqAQA7")#注意:上面的图像数据是一个内嵌的小图标,实际使用时可能需要用实际文件路径或数据#创建一个Button,同时显示图像和文本,设置图像在左边,文本在右边button1=tk.Button(root,text="Info",image=info_icon,compound="left")button1.pack()#创建另一个Button,设置图像在文本下方button2=tk.Button(root,text="Info",image=info_icon,compound="bottom")button2.pack()root.mainloop()```注意:由于图像数据是内嵌的base64编码的字符串,这里为了示例简单使用了一个小图标的内嵌数据,但实际使用时可能并不方便,建议使用文件路径加载图像,例如:```pythoninfo_icon=tk.PhotoImage(file="info.png")```重要:在Tkinter中,由于Python的垃圾回收机制,如果图像对象在函数内部创建且没有被其他引用,可能会被回收而导致显示不出来。因此,通常需要将图像对象保存到全局变量或实例变量中。总结:compound参数控制按钮上文本和图像的相对位置关系,它允许我们选择图像相对于文本的方向(上下左右)或者重叠(center)。如果同时指定了文本和图像,而不设置compound参数,那么默认只显示图像,不显示文本。因此,当需要同时显示文本和图像时,必须设置compound参数。§§相关问题§§1.Tkinter中Button的图像如何加载?2.除了compound参数外,Button还有哪些参数可以控制文本和图像的显示?3.如果希望在Button上同时显示多个图像怎么办?</think> ### Button 的 `compound` 参数详解 在 GUI 编程中,`compound` 参数用于控制按钮中文本与图像的组合布局方式。它是 Tkinter(Python)和某些其他 GUI 框架(如 Java Swing)中的常见配置选项。 #### 核心功能: 当按钮同时包含文本和图像时,`compound` 参数指定它们的相对位置关系。其可选值包括: - `"left"`:图像在文本左侧(默认值) - `"right"`:图像在文本右侧 - `"top"`:图像在文本上方 - `"bottom"`:图像在文本下方 - `"center"`:文本覆盖在图像上(重叠显示) #### 使用方法示例(Python Tkinter): ```python import tkinter as tk from tkinter import PhotoImage root = tk.Tk() # 加载图片资源 icon = PhotoImage(file="icon.png") # 替换为实际路径 # 创建带图像的按钮(文本+图片上下布局) btn = tk.Button( root, text="确认", image=icon, compound="top" # 图像在文本上方 ) btn.pack(padx=20, pady=20) root.mainloop() ``` #### 关键注意事项: 1. **优先级规则**: - 不指定 `compound` 时默认只显示图像(文本不渲染) - 必须同时指定 `text` 和 `image` 参数才会启用组合显示 2. **布局控制**: ```python # 不同组合效果示例 compounds = ["left", "right", "top", "bottom", "center"] for pos in compounds: tk.Button(root, text=pos, image=icon, compound=pos).pack() ``` 3. **框架支持**: - Tkinter:完全支持所有布局选项 - Java Swing:通过 `setHorizontalTextPosition()`/`setVerticalTextPosition()` 实现类似效果 - PyQt:使用 `setIcon()` 和 `setText()` 配合布局管理器控制位置[^1] 4. **设计建议**: - 移动端开发:使用 `top`/`bottom` 布局更符合触控操作习惯 - 桌面端开发:`left`/`right` 布局更符合传统用户习惯 - 重要操作按钮:优先选择无文本的纯图像按钮,提高识别效率 [^1]: PyQt 官方文档:QPushButton 类说明
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值