A1127 | [COCI-2007_2008-contest5]#3 BAZA
来源COCI
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
The longest common prefix of two words is the longest word that both words start with. For example, the longest common prefix of the words "identity" and "idealistic" is the word "ide".
A database contains N words.
The algorithm to search for a query word W in the database is primitive. It compares the word W one by one with each word in the database. Two words are compared letter by letter until a letter in which they differ is found or until the end of one of the words is reached (it is then established either that the words are equal or that one is longer than the other). When the algorithm finds the word W in the database, it terminates.
Analysing the algorithm shows that the number of steps needed to find a word W is equal to the number of words W is compared to, plus the sum of the lengths of the longest common prefixes of W and each of the words it was compared to.
Write a program that calculates the number of steps the algorithm uses to find each of the Q query words.
A database contains N words.
The algorithm to search for a query word W in the database is primitive. It compares the word W one by one with each word in the database. Two words are compared letter by letter until a letter in which they differ is found or until the end of one of the words is reached (it is then established either that the words are equal or that one is longer than the other). When the algorithm finds the word W in the database, it terminates.
Analysing the algorithm shows that the number of steps needed to find a word W is equal to the number of words W is compared to, plus the sum of the lengths of the longest common prefixes of W and each of the words it was compared to.
Write a program that calculates the number of steps the algorithm uses to find each of the Q query words.
输入格式
The first line contains an integer N (1 ≤ N ≤ 30000), the number of words in the database.
Each of the following N lines contains a single word from the database. The words are given in the order the algorithm compares them to a query word. All words in the database will be distinct.
The following line contains an integer Q (1 ≤ Q ≤ 30000), the number of words searched for.
Each of the following Q lines contains a single query word.
All words in the input will be strings of less than 30 lowercase letters of the English alphabet.
Each of the following N lines contains a single word from the database. The words are given in the order the algorithm compares them to a query word. All words in the database will be distinct.
The following line contains an integer Q (1 ≤ Q ≤ 30000), the number of words searched for.
Each of the following Q lines contains a single query word.
All words in the input will be strings of less than 30 lowercase letters of the English alphabet.
输出格式
Output one integer per line for each query word, the number of steps the algorithm uses when searching for the word.
输入输出样例
输入 #1
5 hobotnica robot hobi hobit robi 4 robi hobi hobit rakija
输出 #1
12 10 16 7
输入 #2
8 majmunica majmun majka malina malinska malo maleni malesnica 3 krampus malnar majmun
输出 #2
8 29 14
In the second example, the number of steps to search for the word "krampus" is 8 because the
algorithm only needs to compare its first letter to each word in the database.
When searching for the word "malnar", we need three steps for each of the first three words, and four
steps for each of the remaining five words, for a total of 29 steps.
To find the word "majmun" we use a total of 14 steps. For the first word in the database, we have six
successful comparisons and one step in which we determine that the word "majmunica" is longer than
the query word. For the second word, we also have six successful comparisons and a final step in which
it is established that the words are equal. After finding the word, the algorithm terminates with great
joy.
algorithm only needs to compare its first letter to each word in the database.
When searching for the word "malnar", we need three steps for each of the first three words, and four
steps for each of the remaining five words, for a total of 29 steps.
To find the word "majmun" we use a total of 14 steps. For the first word in the database, we have six
successful comparisons and one step in which we determine that the word "majmunica" is longer than
the query word. For the second word, we also have six successful comparisons and a final step in which
it is established that the words are equal. After finding the word, the algorithm terminates with great
joy.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted