An oceanographic monitoring network deploys lowercase-lettered buoy tags in registration order: 'a', 'b', 'c', ..., 'z'. Long ago the network fixed a binary ping code for every letter, grouped by how early it was registered: the first 2 letters ('a', 'b') each get a 1-bit code, the next 4 ('c' through 'f') each get a 2-bit code, the next 8 ('g' through 'n') each get a 3-bit code, and the final 12 ('o' through 'z') each get a 4-bit code. Within a group, codes are assigned in alphabetical order by counting up in binary from all zeros, so the group's first letter gets the all-zero code of that group's bit-length, its second letter gets that value plus one in binary, and so on.
Written out in full, the fixed table is:
a=0 b=1 c=00 d=01 e=10 f=11 g=000 h=001 i=010 j=011 k=100 l=101 m=110 n=111 o=0000 p=0001 q=0010 r=0011 s=0100 t=0101 u=0110 v=0111 w=1000 x=1001 y=1010 z=1011
A buoy's tag is a string of one or more lowercase letters. Its ping signature is formed by concatenating, in order, the fixed code of every letter in the tag. Because the codes have different lengths, two different tags can occasionally concatenate into the exact same signature string. Given a batch of buoy tags, determine how many distinct ping signatures occur among them.
Line 1: an integer n, the number of buoy tags. Next n lines: one tag per line, consisting only of lowercase English letters.
Print a single integer: the number of distinct ping signatures produced by the n tags.
1 <= n <= 100 1 <= length of each tag <= 20 Each tag consists only of lowercase English letters 'a'-'z'.
Example 1
Input
4 ab d cd dc
Expected
3
Explanation
code(a)='0' and code(b)='1', so 'ab' -> '0'+'1' = '01'. 'd' is the second letter of the 2-bit group (codes 00,01,10,11 for c,d,e,f), so code(d) alone is '01' -- the same signature as 'ab'! 'cd' -> code(c)+code(d) = '00'+'01' = '0001'. 'dc' -> code(d)+code(c) = '01'+'00' = '0100'. So the 4 tags produce signatures {'01','01','0001','0100'}, which is 3 distinct values.
Example 2
Input
3 z z zz
Expected
2
Explanation
'z' is the 12th letter of the 4-bit group (codes 0000 through 1011 for o..z), so code(z) = '1011'. Both occurrences of tag 'z' give the same signature '1011'. Tag 'zz' gives '1011'+'1011' = '10111011', an 8-bit string that differs from '1011'. So the 3 tags produce 2 distinct signatures.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →