Given a number N(1<=N<=2000)., The task is to find the number strings of size N that can be obtained after using characters from 'a' to 'z' and by processing the given q(1<=q<=200000) queries.
For each query given two integers L, R (0<=L<=R<=N) such that substring [L, R] of the generated string of size N must be a palindrome. The task is to process all queries and generate a string of size N such that the substrings of this string defined by all queries are palindrome.
The answer can be very large. So, output answer modulo 1000000007.
Note: 1-based indexing is considered for the string.
Input : N = 3 query 1: (1, 2) query 2: (2, 3) Output : 26 Explanation : Substring 1 to 2 should be palindrome and substring 2 to 3 should be palindrome. so, all three characters should be same. so, we can obtain 26 such strings. Input : N = 4 query 1: (1, 3) query 2: (2, 4) Output : 676 Explanation : substring 1 to 3 should be palindrome and substring 2 to 4 should be a palindrome. So, a first and third character should be the same and second and the fourth should be the same. So, we can obtain 26*26 such strings.
Approach : An efficient solution is to use union-find algorithm.
- Find the mid-point of each range (query) and if there are many queries having the same mid-point then only retain that query whose length is max, i.e (where r – l is max).
- This would have reduced the number of queries to 2*N at max since there is a 2*N number of mid-points in a string of length N.
- Now for each query do union of element l with r, (l + 1) with (r – 1), (l + 2) with (r – 2) and so on. We do this because the character which would be put on the index l would be the same as the one we put on index r. Extending this logic to all queries we need to maintain disjoint-set data structure. So basically all the elements of one component of disjoint-set should have the same letter on them.
- After processing all the queries, let the number of disjoint-set components be x, then the answer is 26^x
- Check if two strings after processing backspace character are equal or not
- Meta Strings (Check if two strings can become same after a swap in one string)
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- String after processing backspace characters
- Print all possible strings of length k that can be formed from a set of n characters
- Count of strings that can be formed using a, b and c under given constraints
- All possible strings of any length that can be formed from a given string
- Count of strings that can be formed from another string using each character at-most once
- Check if given string can be formed by two other strings or their permutations
- Maximum number of strings that can be formed with given zeros and ones
- Check if K palindromic strings can be formed from a given string
- Maximize count of strings of length 3 that can be formed from N 1s and M 0s
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Processing strings using std::istringstream
- Sum of all numbers that can be formed with permutations of n digits
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Count of strings that become equal to one of the two strings after one removal
- Check whether given string can be generated after concatenating given strings
- Find the Initial Array from given array after range sum queries
- Check whether two strings can be made equal by reversing substring of equal length from both strings
Below is the implementation of the above approach :
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.