AMR12D - The Mirror of Galadriel

With water from the stream Galadriel filled the basin to the brim, and breathed on it, and when the water was still again she spoke. 'Here is the Mirror of Galadriel,' she said. 'I have brought you here so that you may look in it, if you will. For this is what your folk would call magic, I believe; though I do not understand clearly what they mean; and they seem also to use the same word of the deceits of the Enemy. But this, if you will, is the magic of Galadriel. Did you not say that you wished to see Elf-magic?' - Galadriel to Frodo and Sam, describing her Mirror.

We call a string S magical if every substring of S appears in Galadriel's Mirror (under lateral inversion). In other words, a magical string is a string where every substring has its reverse in the string.

Given a string S, determine if it is magical or not.


The first line contains T, the number of test cases. The next T lines contain a string each. 


For each test case, output "YES" if the string is magical, and "NO" otherwise.


1 <= T <= 100
1 <= |S| <= 10
S contains only lower-case characters.


Sample Input:

Sample Output:

Notes / Explanation of Sample Input

For the first test case, the list of substrings are : a, b, ab, ba, aba. The reverse of each of these strings is present as a substring of S too.

For the second test case, the list of substring are : a, b, ab. The reverse of "ab", which is "ba" is not present as a substring of the string.

hide comments
aronzx: 2017-08-27 08:52:30

Yes! Just think wisely. However a reckless solution with unordered_map passes as well.O(N^2)

hunnychauhan: 2017-07-12 06:17:57

just check palindrome

ajeetk_973: 2017-03-24 12:13:57

use string library function to do as said ....... then get AC in 0.00 sec

Last edit: 2017-03-24 12:15:32
rohit9934: 2017-03-11 14:48:20

So easy,dont think too much.Its palindrome. setter is trying to make you mad.Accepted in 0.00 seconds

Last edit: 2017-03-11 14:49:15
anurag_tangri: 2017-03-08 12:10:51


nilabja16180: 2017-03-04 11:37:09

Palindrome! AC in ONE GO!

vanvinhbk94: 2017-02-21 04:35:32

AC in one go

cake_is_a_lie: 2017-02-15 13:45:27

The problem statement is correct and the string length can NOT be up to 100 as the comments suggest, at least not as of now. I got AC on the first try but added the following line of code to the solution to check:
if (s.length() > 10) cout << 1 / (s.length() - s.length());
no SIGFPE => no string over 10 characters long.

For those people having that problem, did you remember to reserve an extra character for the ending '\0'? i.e. you should have declared char s[11];

agarwalg271: 2017-01-21 15:12:43

my 100th . don't use fast i/o it gives me 4 WA's

Last edit: 2017-01-21 15:13:28
kass_97: 2017-01-14 09:16:19

Piece of cake ;)

Added by:Varun Jalan
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Varun Jalan - ICPC Asia regionals, Amritapuri 2012