一道ACM題目
一道ACM題目
5036 Match WordsTimeLimit : 1 Second Memorylimit : 32 Megabyte
Totalsubmit : 273 Accepted : 49
xiaoz wants to remember more words.So he asks you ,as a member of a development team for a words-matching program,
to write a module that will check if a given word can match some words of a known dictionary. The dictionary is
made of words.A word w matchs a word v if and only if there is some permutation p of character positions that
takes w to v.For example,"caret" can match "cater",but "caret" can't match "certe".
Input
The first part of the input contains all words from the dictionary. Each word occupies its own line. This part is
finished by the single character '#' on a separate line. All words are different. There will be at most 20000 words
in the dictionary.
The next part of the file contains all words that are to be checked. Each word occupies its own line. This part is
also finished by the single character '#' on a separate line. There will be at most 2000 words that are to be checked.
All words in the input (words from the dictionary and words to be checked) consist only of small alphabetic characters
and each one contains 30 characters at most.
Output
Write to the output exactly one line for every checked word in the order of their appearance in the second part of the
input file.Write the checked word first,then write the character ':',and after a signle space write all its possible
words ,from the dictionary,which can match the checked word.The matching words should be written in alphabetical order.
If there are no matching words for the checked word then the line feed should immediately follow the colon.
Sample Input
undisplayed
trace
tea
singleton
eta
eat
displayed
crate
cater
carte
caret
beta
beat
bate
ate
abet
#
retac
btae
good
displayde
hello
#
Sample Output
retac: caret carte cater crate trace .
btae: abet bate beat beta .
good:
displayde:displayed .
hello:
我的思路是先讀入字典中的字符串,存起來.然后再讀入用來檢查的字符串,存起來.然后,將字典中的字符串和用來檢查的字符串分別按字母排序后形成新的字符串.然后根據(jù)新形成的用來檢查的字符串一個一個地與新形成的字典中的字符串比較,若相同則保存,不相同則忽略.
最后將比較后相同的字符串用一個冒泡排序進(jìn)行排序.然后按題目要求輸出.
輸出格式?jīng)]有問題,但是超時,誰知道應(yīng)該在哪個地方進(jìn)行優(yōu)化啊?
5036 Match WordsTimeLimit : 1 Second Memorylimit : 32 Megabyte
Totalsubmit : 273 Accepted : 49
xiaoz wants to remember more words.So he asks you ,as a member of a development team for a words-matching program,
to write a module that will check if a given word can match some words of a known dictionary. The dictionary is
made of words.A word w matchs a word v if and only if there is some permutation p of character positions that
takes w to v.For example,"caret" can match "cater",but "caret" can't match "certe".
Input
The first part of the input contains all words from the dictionary. Each word occupies its own line. This part is
finished by the single character '#' on a separate line. All words are different. There will be at most 20000 words
in the dictionary.
The next part of the file contains all words that are to be checked. Each word occupies its own line. This part is
also finished by the single character '#' on a separate line. There will be at most 2000 words that are to be checked.
All words in the input (words from the dictionary and words to be checked) consist only of small alphabetic characters
and each one contains 30 characters at most.
Output
Write to the output exactly one line for every checked word in the order of their appearance in the second part of the
input file.Write the checked word first,then write the character ':',and after a signle space write all its possible
words ,from the dictionary,which can match the checked word.The matching words should be written in alphabetical order.
If there are no matching words for the checked word then the line feed should immediately follow the colon.
Sample Input
undisplayed
trace
tea
singleton
eta
eat
displayed
crate
cater
carte
caret
beta
beat
bate
ate
abet
#
retac
btae
good
displayde
hello
#
Sample Output
retac: caret carte cater crate trace .
btae: abet bate beat beta .
good:
displayde:displayed .
hello:
我的思路是先讀入字典中的字符串,存起來.然后再讀入用來檢查的字符串,存起來.然后,將字典中的字符串和用來檢查的字符串分別按字母排序后形成新的字符串.然后根據(jù)新形成的用來檢查的字符串一個一個地與新形成的字典中的字符串比較,若相同則保存,不相同則忽略.
最后將比較后相同的字符串用一個冒泡排序進(jìn)行排序.然后按題目要求輸出.
輸出格式?jīng)]有問題,但是超時,誰知道應(yīng)該在哪個地方進(jìn)行優(yōu)化啊?
英語人氣:408 ℃時間:2020-09-29 20:30:14
優(yōu)質(zhì)解答
哈工程上的題目?做的時間有點(diǎn)長了,忘記了,不過代碼還在.#includeusing namespace std;char a[20002][31],b[20002][31];int n;struct Node{char *pa,*pb;}c[20002];bool cmp(Node x,Node y){int t = strcmp(x.pb,y.pb...
我來回答
類似推薦
猜你喜歡
- 1如何理解矛盾的兩種基本屬性在事物發(fā)展中的作用
- 2以《冬天來了 ,春天還會遠(yuǎn)嗎?》為題 主要是寫不怕困難,就離成功不遠(yuǎn)了
- 3英語17.-Are you going to have a holiday this year?
- 4橢圓C方程為(x^2)/8 +(Y^2)/4=1,若直線y=x+m與橢圓C交于不同的兩點(diǎn)A,B,且線段AB的中點(diǎn)M關(guān)于直線y=x+1的對稱點(diǎn)在圓X^2+Y^2=1上,求m的值
- 5用愿意.就.,愿意.就.造句
- 6下列說法中屬于控制噪音聲源的是 ( )屬于阻擋噪音傳播的措施是( )屬于防止噪聲進(jìn)入人耳的措施是
- 7仿寫句子,用上草長鶯飛
- 8Here is a ticket to the movie for you.You are____(luck).填什么?為什么添這個?
- 9某元素的一種粒子的結(jié)構(gòu)示意圖為,下列說法錯誤的是( ?。?A.該粒子屬于原子 B.該元素在化合物中顯+1價 C.該元素的一個離子含有11個電子 D.該元素的原子在化學(xué)反應(yīng)中容易失去電子
- 10中國地球有多大?
- 11幾個關(guān)于餐廳英語用法的問題
- 12圓圓的爸爸去銀行取款,第一次取了存款的一半還多5元,第二次取了余下的一半還少10元,還剩135元,一共多少