Блог пользователя Pawan

Автор Pawan, история, 7 лет назад, По-английски

PROBLEM:

"Given an array of strings, find the string which is made up of maximum number of other strings contained in the same array."

INPUT :

[ “rat”, ”cat”, “abc”, “xyz”, “abcxyz”, “ratcatabc”, “xyzcatratabc” ]

OUTPUT:

“xyzcatratabc”

Explaination :

“abcxyz” contains 2 other strings
“ratcatabc” contains 3 other strings,
“xyzcatratabc” contains 4 other strings"

I want to know what is the best time complexity with can solve this problem and how ?
I shall be highly thankful to codeforces community!

  • Проголосовать: нравится
  • +17
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Maybe Aho-corasick

»
7 лет назад, # |
Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

I can think of a solution with string matching and DAG longest path with approximate time complexity O(n * m), where m = sum of all strings' length, and n = number of strings. (you will also need some extra memory) Firstly, you need a linear string matching algorithm, I would use KMP (https://es.wikipedia.org/wiki/Algoritmo_Knuth-Morris-Pratt). Also be familiar with DAG longest path problem (http://www.geeksforgeeks.org/find-longest-path-directed-acyclic-graph/).

For each string A do the following:

For every string B with smaller length than A:

Use KMP to find B occurrences in A. Now imagine A as a DAG (https://en.wikipedia.org/wiki/Directed_acyclic_graph) where each index is a node. So for each occurence of B in index i, store an edge E(i -> i + B.length). This is where you need extra memory.

After matching all strings B smaller than A, find longest path in A from index 0 to index A.length — 1. If there is no such path, A cannot be made up entirely of other strings. Otherwise, longest path value is the biggest number of strings that can form A. So the string that has the biggest result is your answer.

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

This problem can easily be solved in linear time using the Aho-Corasick Algorithm.

  1. Build a trie with all the strings as words in the data structure.
  2. Use the Aho-Corasick algorithm to build a string matching automaton.
  3. Run the automaton on each string of the dictionary and check the number of matches.

Total complexity: time and space.