Submit | All submissions | Best solutions | Back to list |
STRSTR - String! String! String! |
In computer science, String is one of the most important, interesting topics. String is used in databases, compiler, bio-informatics etc. As a programmer and problem solver you should know about the string algorithms. There are several string algorithms and I am not going to discuss none of them. I am just telling you the names so that you can check those algorithms later.
- Knuth–Morris–Pratt algorithm
- Z algorithm
- Rabin Karp Algorithm
- Aho Corasick String Matching Algorithm
- Manacher Algorithm
- Boyer–Moore string search algorithm
And there are some data structures on string, they are
- Trie Tree
- Suffix Array
- Suffix Trie
- Suffix Tree
- Rope Data Structure
Now we need an algorithm which can be re-invented by you to solve the following problems. And that is...
You will be given several strings, for each string you need to find the closest string for that string. What is a closest string? A string is closest string of another string if prefix match between them is maximum and obviously not considering the string itself (BTW, this definition is mine, it can be different for different person J. But you have to follow this definition to solve this problem). Let’s say you have four strings:
- ABABABA
- ABCDDAA
- ABABKKK
- BAAWEE
For string 1, the closest string is the string numbered 3.
For string 2, the closest string is the string numbered 1 and 3.
For string 3, the closest string is the string numbered 1
For string 4, there is no closest string.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case starts with a line containing an integer N (1 ≤ N ≤ 100) denoting the number of strings.
Each string will contain only the UPPERCASE English letters (A-Z). And each string will have length at most 100. And different strings may have different lengths. You can assume that there will be no spaces, symbols, punctuation marks in the input strings.
Output
For each case, print the case number in a single line. Then for each given string, print the index of the closest string in a single line. If there are multiple strings which are closest for a certain string print the minimum index. Indexes are starting from 1. And if there is no closest string then print -1. Check the output for the sample input for clear understanding.
Sample
Input 3 4 ABABABA ABCDDAA ABABKKK BAAWEEE 1 A 3 A A A Output Case 1: 3 1 1 -1 Case 2: -1 Case 3: 2 1 1
Note: Prefix is a substring of a string which is starting from the beginning. For ABCD,
- A
- AB
- ABC
- ABCD
are the prefixes. BCD is not a prefix of ABCD.
Added by: | Faiyaz |
Date: | 2014-06-25 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | Own Problem |