#P7050. Concatenation of Prefix and Suffix
Concatenation of Prefix and Suffix
Concatenation of Prefix and Suffix
Gennady, a famous programmer, enjoys creating new words by an innovative method. Instead of simply concatenating two words, he constructs a new word by taking a non-empty prefix of the first word and a non-empty suffix of the second word, then concatenating them. For example, given the words cat
and dog
, the resulting words include cog
, catdog
, caog
, among others.
The problem is to determine how many different words can be generated by this method.
The process can be described formally as follows: Given two strings \(A\) and \(B\), count the number of distinct strings obtained by concatenating a non-empty prefix of \(A\) and a non-empty suffix of \(B\). That is, count the size of the set
[ {A[1..i] + B[j..|B|] : 1 \leq i \leq |A|, 1 \leq j \leq |B|} ]
where A[1..i]
denotes the first \(i\) characters of \(A\), and B[j..|B|]
denotes the substring of \(B\) from the \(j\)th character to the end.
inputFormat
The input consists of two lines. The first line contains the first word, and the second line contains the second word. Both words are non-empty strings composed of lowercase English letters.
outputFormat
Output a single integer representing the number of distinct words that can be created using Gennady's method.
sample
cat
dog
9