An algorithm (or a pointer to a literature) was looking for a string processing call

A group of funny students write essays exclusively by the plagiarized parts of the complete works of WIlliam Shakespere. At one end of the scale, an essay can consist solely of a verbatim copy of a monologue ... on the other hand, you can see that the work is so novel that - when using the common alphabet - none of the two adjacent characters in the essay were used in the vicinity of Will.

The essay needs to be evaluated. Grade 1 is assigned any essay that can be found (identical in nature) in the text of the complete works. Grade 2 is assigned any work that can be successfully built from at least two different (identical in nature) passages in complete works, etc ... to the limit - for an essay with N characters that evaluate N if and only if in two works there were no two adjacent characters in the essay.

The challenge is to implement a program that can efficiently (and accurately) evaluate essays. Although any (practically feasible) data structure for representing complete works is acceptable - essays are presented in the form of ASCII strings.

After considering this tantalizing question for a while, I came to the conclusion that this is much more complicated than it seems. The naive solution for an essay of length N includes 2 ** (N-1) workarounds, which is too inefficient to be practical.

Although, obviously, I am interested in the proposed solutions - I would also like to draw attention to any literature that deals with this problem or any similar problem.

EXPLANATIONS

Perhaps some examples (starting with shorter lines) will help clarify the "grade" for the "essay"?

Suppose the full Shakespere works are shortened to:

"The quick brown fox jumps over the lazy dog."

1 " " " ". "jogging" 6 ( , ), 6 ... , : [J] [OG] [] [] [] [] ". - " " - .

, 2 * (N-1) . , (N-1) N , ..., ~ 2 * (N-1) .

An (N) DFA - . , -, "", , . , , ( ). , , (N) DFA , /.

+4
4

, , , . , .

, - , , , , N - , ?

, . k , k . , , x..k , x, , . , k , 1..x-1 ( ) 1. k = 1... , .

( StackOverflow , , , StackOverflow: -)).

, , , , , . , , , , , .

, . , , , , . , . , , , , , .

- , , - , , - , , , , . , - , , , , , - , , , , . , , - . , , , , , , , , - , , , , . , , , , .

+3

. . , , , 2 ^ N. - O (N).

trie . 5 6. , ( trie) , .

+1

.

, , , , .

NDFA DFA. 2 ^ n , , , ?

. NDFA M + C; C, C = 26 * 2 + #punctuation M , (re) , 0 . , DFA O (2 ^ M) , , DFA, . , ( , ). , M/C, M , NDFA M . , .

1, , .

, DFA. , ( ), () .

0

Source: https://habr.com/ru/post/1525726/


All Articles