🏡 index : github.com/captn3m0/codechef.git

---
{"category_name":"easy","problem_code":"SSTRPREF","problem_name":"Prefix as a Substring","problemComponents":{"constraints":"- $1 \\leq T \\leq 10^5$\n- $1 \\leq |X|, |S_1|, |S_2| \\leq 10^6$\n- Sum of $|X|$ over all test cases doesn\u0027t exceed $10^6$.\n- Sum of $|S_1|$ over all test cases doesn\u0027t exceed $10^6$.\n- Sum of $|S_2|$ over all test cases doesn\u0027t exceed $10^6$.","constraintsState":true,"subtasks":"","subtasksState":true,"inputFormat":"- First line of the input contains $T$, the number of test cases. Then the test cases follow.\n- Each test case contains three lines, string $S_1$ in the first line, string $S_2$ in the second line and string $X$ in the third line. All strings consist of only lowercase Latin letters.\n","inputFormatState":true,"outputFormat":"For each test case, print a single line containing one integer denoting the number of substrings of $X$ that satisfy the above conditions.","outputFormatState":true,"sampleTestCases":{"0":{"id":1,"input":"3\nab\nbc\nabc\naa\nbb\nab\naab\nacb\nbcaabacbc","output":"5\n3\n10","explanation":"**Test case 1:**\n\nThere are 6 substrings of $X = abc$ namely $[a, b, c, ab, bc, abc]$.\n\n- Observe that $a$ is a prefix of $S_1 = ab$ and an empty string is a prefix of $S_2 = bc$. By concatenating them, we get the string $a$.\n- Now an empty string is a prefix of $S_1$ and $b$ is a prefix of $S_2$. And by concatenating them, we get the string $b$.\n- Observe that there is no prefix of $S_1$ starting with $c$ and also no prefix of $S_2$ starting with $c$. So we cannot express the string $c$ as a concatenation of prefixes.\n- The string $ab$ is a prefix of $S_1$ and an empty string is a prefix of $S_2$. And by concatenation, we get $ab$.\n- Similarly, an empty string is a prefix of $S_1$ and $bc$ is a prefix of $S_2$. And by concatenating them, we get $bc$.\n- Finally, by taking the prefix $a$ of $S_1$ and the prefix $bc$ of $S_2$, we get $abc$ when we concatenate them.\n\nSo all substrings of $X$ except for the substring $c$ can be expressed as a concatenation of some prefix of $S_1$ and some prefix of $S_2$. Therefore, the answer for this test case is $5$.\n\n\n**Test case 2:**\n\n\nThere are 3 substrings of $X = ab$ namely $[a, b, ab]$.\n\n- Observe that $a$ is a prefix of $S_1 = aa$ and an empty string is a prefix of $S_2 = bb$. By concatenating them, we get the string $a$.\n- An empty string is a prefix of $S_1$ and $b$ is a prefix of $S_2$. And by concatenating them, we get the string $b$.\n- Finally, by taking the prefix $a$ of $S_1$ and the prefix $b$ of $S_2$, we get $ab$ when we concatenate them.\n\nSo all substrings of $X$ can be expressed as a concatenation of some prefix of $S_1$ and some prefix of $S_2$. Therefore, the answer for this test case is $3$.","isDeleted":false}}},"video_editorial_url":"https://youtu.be/Rw5xRzTlMjM","languages_supported":{"0":"CPP14","1":"C","2":"JAVA","3":"PYTH 3.6","4":"CPP17","5":"PYTH","6":"PYP3","7":"CS2","8":"ADA","9":"PYPY","10":"TEXT","11":"PAS fpc","12":"NODEJS","13":"RUBY","14":"PHP","15":"GO","16":"HASK","17":"TCL","18":"PERL","19":"SCALA","20":"LUA","21":"kotlin","22":"BASH","23":"JS","24":"LISP sbcl","25":"rust","26":"PAS gpc","27":"BF","28":"CLOJ","29":"R","30":"D","31":"CAML","32":"FORT","33":"ASM","34":"swift","35":"FS","36":"WSPC","37":"LISP clisp","38":"SQL","39":"SCM guile","40":"PERL6","41":"ERL","42":"CLPS","43":"ICK","44":"NICE","45":"PRLG","46":"ICON","47":"COB","48":"SCM chicken","49":"PIKE","50":"SCM qobi","51":"ST","52":"SQLQ","53":"NEM"},"max_timelimit":1,"source_sizelimit":50000,"problem_author":"suryaprak_adm","problem_tester":"","date_added":"23-09-2021","tags":{"0":"greedy","1":"medium","2":"range","3":"start13","4":"suryaprak_adm","5":"z"},"problem_difficulty_level":"Unavailable","best_tag":"Z Algorithm","editorial_url":"https://discuss.codechef.com/problems/SSTRPREF","time":{"view_start_date":1632663002,"submit_start_date":1632663002,"visible_start_date":1632663002,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=SSTRPREF","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
Given $3$ string $S_1$, $S_2$ and $X$, find the number of non-empty substrings of $X$ which can expressed as $P + Q$ i.e. concatenation of strings $P$ and $Q$, where $P$ is some prefix of $S_1$ (possibly empty) and $Q$ is some prefix of $S_2$ (possibly empty).



A **substring** of a string is a contiguous subsequence of that string. For example, "chef" is a substring of "codechef", but "def" is not.


A **prefix** of a string $S$ is a substring of $S$ that occurs at the beginning of $S$. For example, "code" is a prefix of "codechef", but "chef" is not. Also, an empty string is a prefix of any string.
<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>