## Problem Summary:

**Problem Number:** 1141

**Problem Name:** Growing Strings

**Author’s Name:** by Pablo Ariel Heiber Argentina

**Timelimit:** 5

**Problem Category:** AD-HOC

**Problem Source:** https://www.urionlinejudge.com.br/judge/en/problems/view/1141

## Some Talks about Contest Programming:

An incredible method to enhance your abilities when figuring out how to code is by solving coding problems. Solving different kinds of challenges and riddles can enable you to improve as a problem solver, take in the complexities of a programming dialect, get ready for prospective job interviews, learn new algorithms and more.

An online judge is an online platform to test programs in focused programming challenges. They are likewise used to practice for such challenges. A considerable amount of these platforms also arrange their own programming contests.

## 10 Steps to Solve Any Problems:

- Read the problem completely at least two or three times (or however many makes you feel comfortable)
- Identify the subject, the problem belongs to. Is it a sorting or pattern matching problem? Can I use graph theory? Is it related to number theory? etc.
- Try to solve the problem manually by considering 3 or 4 sample data sets.
- After concentrate on optimizing the manual steps. Try to make it as simple as possible.
- Write to write pseudo-code and comments besides the code from the manual steps. One thing you can do is to check after every function is written. Use a good IDE with a debugger, if possible. Don’t need to think much about the syntax. Just focus on the logic and steps.
- Replace the comments or pseudo-code with real code. Always check if the values and code are behaving as expected before moving to the new line of pseudo-code.
- Then optimize the real code.
- Take care of boundary conditions as well.
- Get feedback from your teammates, professors, and other developers and also ask your question on Stack Overflow if possible. Try to learn from others’ guidelines and what they are handling those problems. A problem may be solved in several ways. So, don’t get disappointed if you can’t think like an expert. You need to stick to the problem and you will gradually become better and quicker in solving problems like others.
- Practice, Practice, and Practice.

**N.B: Try to follow the above steps always. If you still can’t get the problem solved, take a look at the solution below. Don’t just copy paste the code. It will kill your creativity. Try to enjoy contest programming and develop your skills.**

## Solution:

#include <bits/stdc++.h> using namespace std; #define fst first #define snd second typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; #define pb push_back #define for_tests(t, tt) int t; scanf("%d", &t); for(int tt = 1; tt <= t; tt++) template<typename T> inline T abs(T t) { return t < 0? -t : t; } const ll modn = 1000000007; inline ll mod(ll x) { return x % modn; } const int MAX = 1000100; int tr[MAX][26]; int mt[MAX], pr[MAX]; int st[MAX]; char s[10003][1003]; int p[10009], sz[10009], memo[10009]; int main() { int i, j, k, m; while(scanf("%d", &m) == 1 && m) { for(i = 0; i < m; i++) { scanf("%s", s[i]); sz[i] = strlen(s[i]); p[i] = i; } sort(p, p + m, [](int i, int j) { return sz[i] < sz[j]; }); memset(tr[0], 0, sizeof(int)*26); int tn = 0; for(i = 0; i < m; i++) { char *c = s[i]; int v = 0; for(j = 0; c[j]; j++) { if(!tr[v][c[j]-'a']) { tr[v][c[j]-'a'] = ++tn; memset(tr[tn], 0, sizeof(int)*26); mt[tn] = 0; } v = tr[v][c[j]-'a']; } mt[v] = 1; st[v] = i; } queue<int> q; for(i = 0; i < 26; i++) if(tr[0][i]) q.push(tr[0][i]), pr[tr[0][i]] = 0; while(!q.empty()) { int x = q.front(); q.pop(); for(i = 0; i < 26; i++) { if(!tr[x][i]) continue; int &p = pr[tr[x][i]]; p = pr[x]; while(p && !tr[p][i]) p = pr[p]; p = tr[p][i]; if(mt[p]) mt[tr[x][i]] |= 2; q.push(tr[x][i]); } } int mx = 0; for(i = 0; i < m; i++) { k = p[i]; char *c = s[k]; int n = sz[k]; int v = 0; memo[k] = 1; for(j = 0; j < n; j++) { v = tr[v][c[j]-'a']; int x = v; if(j == n - 1) x = pr[x]; while(mt[x]) { if(mt[x] & 1) memo[k] = max(memo[k], 1 + memo[st[x]]); x = pr[x]; } } mx = max(mx, memo[k]); } printf("%dn", mx); } }

**N.B.: Code is Collected from Different Sources**

- How to Create a RGB Color Picker for Images using OpenCV Python - 22 April, 2022
- Combine Several Images Vertically with Padding using OpenCV Python - 21 April, 2022
- Combine Several Images Horizontally with Padding using OpenCV Python - 21 April, 2022