What is the best data structure and algorithm for comparing a list of strings?
Posted
by
Chiraag E Sehar
on Stack Overflow
See other posts from Stack Overflow
or by Chiraag E Sehar
Published on 2010-12-25T09:24:04Z
Indexed on
2010/12/25
9:54 UTC
Read the original article
Hit count: 271
I want to find the longest possible sequence of words that match the following rules:
- Each word can be used at most once
- All words are Strings
- Two strings
sa
andsb
can be concatenated if the LAST two characters ofsa
matches the first two characters ofsb
.
In the case of concatenation, it is performed by overlapping those characters. For example:
- sa = "torino"
- sb = "novara"
- sa concat sb = "torinovara"
For example, I have the following input file, "input.txt":
novara
torino
vercelli
ravenna
napoli
liverno
messania
noviligure
roma
And, the output of the above file according to the above rules should be:
torino
novara
ravenna
napoli
livorno
noviligure
since the longest possible concatenation is:
torinovaravennapolivornovilligure
Can anyone please help me out with this? What would be the best data structure for this?
© Stack Overflow or respective owner