# YVSTR Editorial

Author and Editorialist: Hussain Kara Fallah
Tester: Michael Nematollahi

sorting

### PROBLEM:

A string is called special if the set of all of its non-empty different subsequences IS equal to the set of all of its non-empty substrings. Refer to the problem statement for more detailed explanation.

Now given a string S with length up to 10^6. Count the number of its distinct special non-empty substrings.

### EXPLANATION:

We should figure first what are the possible forms of a special string?

Clearly, a string consisting of 1 letter only is special (like âaaaaâŚâ). What about more than 1 letter?

If the string consists of more than 2 letters itâs not special. Consider our stringâs letters from left to right. Take the first appearing letter and the last appearing letter (if we consider the first appearance of each letter, the last letter that appears).

In case (âaabbbccaaâ) the first is âaâ and the last is 'câ

In case (âabcccddbbâ) the first is âaâ and the last is 'dâ

Now take the subsequence consisting of all occurrences of both letters. In the first case itâs âaaccâ, in the second itâs âaddâ. This subsequence doesnât appear as a substring, because thereâs at least one extra different letter that appeared before our âlast appearingâ letter. So as a result, the substring containing all occurrences of both letters will contain the extra letter.

(Note these are only detailed examples, but the observation can be applied to any string).

Now we are left with strings consisting of 2 letters. A string of 2 letters is special if it is only like (âaaaaâŚbbbbâŚâ) (the first letter repeated X times followed by the second letter repeated Y times.

Letâs assume that it doesnât follow the mentioned form. (Letâs suppose that our string consists of âaâ and âbâ only). According to our assumption, then there must be at least one letter âbâ with at least one âaâ to the left and at least one âaâ to the right. So if we take the subsequence of all occurrences of letter âaâ itâs not present as a substring.

Now how to answer our question?

First letâs break our string into the minimum number of disjoint parts, and each part consisting of consecutive identical letters.

For example âaaabbccddeffâ will be broken into {âaaaâ,âbbâ,âccâ,âddâ,âeâ,âffâ}.

The single letter case is easy, we maintain the largest group occurred for each letter and add up their sizes.

Now, the 2 letters case can be achieved from taking 2 adjacent groups (letâs say the first one size is x and the second is y so we can have x*y special substrings.

But we need to count only the distinct ones. Letâs maintain for each pair of letters (let_1,let_2) , a vector containing pairs (x,y) such that some point in our string there was a sequence of x letters of let1 followed by y letters of let2 (consecutively). For each pair, maintain all occurrences of matching consecutive groups. In the previous example, some possible groups are (âaaaâ,bb") , (âddâ,âeâ) , and so.

For each pair of letters (let1,let2) letâs count the number of distinct special substrings formed only by these letters (and let1 is the first letter). Letâs consider our lettersâ vector (the letters pair vector).

Letâs suppose that we have 2 pairs (x1,y1) , (x2,y2) such that (x1 >= x2 and y1 >= y2), then clearly the second pair is redundant, because all substrings formed from the groups of our second pair can be formed from the groups of the first. Letâs remove all redundant pairs (Hint: sort them according to x values and maintain decreasing y values).

Now for every 2 pairs in our vector (x1,y1) , (x2,y2) if (x1 > x2) then it implies that (y1 < y2). Now, all pairs are sorted according to the value of x. Letâs take two consecutive pairs as mentioned, what substrings should we add from the first pair that wonât occur from any other pair in the vector? Itâs clearly *(x1-x2)y1. (Assuming the first pair is (x1,y1) and the second is (x2,y2)).

We do the mentioed process for all pairs of letters.

Complexity O(|S| \, log \, |S|)

### AUTHORâS AND TESTERâS SOLUTIONS:

AUTHORâs solution

TESTERâs solution

1 Like

This is a must do problem for all types of people in the world of programming. To beginners, it teaches how to solve effectively the latter part of solution and how to approach difficult looking but not so difficult problems, and to the little-experienced ones, it teaches how to reduce the problem quickly.

Can someone please tell whatâs wrong in my

``````
[1]. I'm fed up with this problem and the WAs :(

[1]: https://www.codechef.com/viewsolution/22083802``````

This has been iterated many times but it is not a good practice to directly ask âplease debug my codeâ. Debugging is an art which one can learn only through practice. Grab that opportunity to debug. You know your code much better than others and if it is difficult to debug for you, it is much more difficult for others who donât even know what you wanted to do. No offence, though. Hope you understand.

He already has a bunch of WAâs in it, which means he already tried enough. In these cases, its better to ask for help. Perhaps someone else, who can provide an outsiderâs PoV, can debug it easily - saving hours and hours of your time.

2 Likes

Sorry for not sharing my approach, but I already put up a question regarding the same yesterday, but didnât get much response so thought of asking on the Editorial Page. If someone happens to see this thread, please go through this https://discuss.codechef.com/questions/142721/help-needed-in-yalalovichik-strings-yvstr-december-cookoff to know my working.

And sorry if I violated any norms, was just fed up after trying over 15 times so thought someone might help.