Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> const int N = 10000 + 5; bool dp[N][5]; int main() { std::string str; std::cin >> str; int n = str.length(); memset(dp, false, sizeof(dp)); std::vector<std::string> answer(0); for (int i = n - 1; i >= 5; i--) { for (int j = 2; j <= 3; j++) { if (i + j == n) { dp[i][j] = true; } else if (i + j < n) { for (int k = 2; k <= 3; k++) { dp[i][j] |= dp[i + j][k] && str.substr(i, j) != str.substr(i + j, k); } } if (dp[i][j]) { answer.push_back(str.substr(i, j)); } } } std::sort(answer.begin(), answer.end()); answer.erase(std::unique(answer.begin(), answer.end()), answer.end()); printf("%d\n", (int)answer.size()); for (auto &it : answer) { std::cout << it << std::endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; string S; set<string> ans; int N; bool u[10001][2]; void dfs(int n, string prev, int p) { if (u[n][p]) return; if (n + 1 - 2 > 4) { string t = S.substr(n - 1, 2); if (t != prev) { ans.insert(t); dfs(n - 2, t, 2); } } if (n + 1 - 3 > 4) { string t = S.substr(n - 2, 3); if (t != prev) { ans.insert(t); dfs(n - 3, t, 3); } } u[n][p] = 1; } int main() { cin >> S; N = S.length(); memset(u, 0, sizeof(u)); dfs(S.length() - 1, "", 0); cout << ans.size() << endl; for (__typeof((ans).begin()) i = (ans).begin(); i != (ans).end(); i++) cout << *i << endl; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s; int n = s.length(); if (n <= 6) { cout << "0\n"; return 0; } bool dp2[10001] = {0}, dp3[10001] = {0}; set<string> ans; t.push_back(s[n - 2]); t.push_back(s[n - 1]); ans.insert(t); if (n >= 8) { t = ""; t.push_back(s[n - 3]); t.push_back(s[n - 2]); t.push_back(s[n - 1]); ans.insert(t); } dp2[n - 2] = dp3[n - 3] = 1; for (int j = n - 4; j >= 5; j--) { if (j + 1 < n - 2) { string curr, m; curr.push_back(s[j]); curr.push_back(s[j + 1]); m.push_back(s[j + 2]); m.push_back(s[j + 3]); if ((dp2[j + 2] && curr != m) || dp3[j + 2]) { dp2[j] = 1; ans.insert(curr); } else dp2[j] = 0; } if (j + 2 < n - 2) { string curr, m; curr.push_back(s[j]); curr.push_back(s[j + 1]); curr.push_back(s[j + 2]); m.push_back(s[j + 3]); m.push_back(s[j + 4]); m.push_back(s[j + 5]); if ((curr != m && dp3[j + 3]) || dp2[j + 3]) { ans.insert(curr); dp3[j] = 1; } else dp3[j] = 0; } } cout << ans.size() << endl; for (set<string>::iterator it = ans.begin(); it != ans.end(); it++) cout << *it << endl; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 1000006; char s[N]; int n; set<string> vis[N]; set<string> ss; void f(int i, const string& last) { if (vis[i].count(last)) return; vis[i].insert(last); if (i - 2 >= 4) { string tmp = ""; tmp += s[i - 1]; tmp += s[i]; if (tmp != last) { ss.insert(tmp); f(i - 2, tmp); } } if (i - 3 >= 4) { string tmp = ""; tmp += s[i - 2]; tmp += s[i - 1]; tmp += s[i]; if (tmp != last) { ss.insert(tmp); f(i - 3, tmp); } } } int main() { scanf(" %s", s); n = strlen(s); f(n - 1, ""); printf("%d\n", ss.size()); for (string x : ss) printf("%s\n", x.c_str()); return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; int n = str.length(); vector<bool> dp2(str.length(), false); vector<bool> dp3(str.length(), false); set<string> codewords; if (n >= 7) { dp2[n - 2] = true; codewords.insert(str.substr(n - 2, 2)); } if (n >= 8) { dp3[n - 3] = true; codewords.insert(str.substr(n - 3, 3)); } for (int i = n - 4; i > 4; i--) { if ((dp2[i + 2] && !(str[i] == str[i + 2] && str[i + 1] == str[i + 3])) || dp3[i + 2]) { dp2[i] = true; codewords.insert(str.substr(i, 2)); } if ((dp3[i + 3] && (str[i] != str[i + 3] || str[i + 1] != str[i + 4] || str[i + 2] != str[i + 5])) || dp2[i + 3]) { dp3[i] = true; codewords.insert(str.substr(i, 3)); } } cout << codewords.size() << endl; for (auto it = codewords.begin(); it != codewords.end(); ++it) std::cout << *it << endl; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int MaxN = 1e5 + 10; vector<string> tot[MaxN]; map<string, int> c; int cnt; char s[MaxN]; bool check[MaxN], flag; set<string> ans; int main() { scanf("%s", s); int len = strlen(s); check[len] = 1; for (int i = len - 2; i >= 5; i--) { if (check[i + 2]) { string t; t += s[i]; t += s[i + 1]; if (tot[i + 2].size() == 0) { check[i] = 1; tot[i].push_back(t); ans.insert(t); } else { flag = 1; for (int j = 0; j < tot[i + 2].size(); j++) { if (tot[i + 2][j] != t) { flag = 0; break; } } if (!flag) { tot[i].push_back(t); ans.insert(t), check[i] = 1; } } } if (check[i + 3]) { string t; t += s[i]; t += s[i + 1]; t += s[i + 2]; if (tot[i + 3].size() == 0) { check[i] = 1; tot[i].push_back(t); ans.insert(t); } else { flag = 1; for (int j = 0; j < tot[i + 3].size(); j++) { if (tot[i + 3][j] != t) { flag = 0; break; } } if (!flag) { tot[i].push_back(t); ans.insert(t), check[i] = 1; } } } } printf("%d\n", ans.size()); for (auto &ite : ans) { cout << ite << endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4 + 10; char str[maxn]; string tmp; set<string> ans; int dp[maxn][2]; int main() { scanf("%s", str + 1); int len = strlen(str + 1); tmp = " "; for (int i = 1; i <= len; i++) tmp += str[i]; tmp[len + 1] = '\0'; for (int i = len - 1; i >= 6; i--) { if (i + 1 == len) dp[i][0] = 1; else if (i + 2 == len) dp[i][1] = 1; else { if (dp[i + 2][1] || (dp[i + 2][0] && (str[i] != str[i + 2] || str[i + 1] != str[i + 3]))) dp[i][0] = 1; if (dp[i + 3][0] || (dp[i + 3][1] && (str[i] != str[i + 3] || str[i + 1] != str[i + 4] || str[i + 2] != str[i + 5]))) dp[i][1] = 1; } } for (int i = 6; i <= len - 1; i++) { if (dp[i][0]) ans.insert(tmp.substr(i, 2)); if (dp[i][1]) ans.insert(tmp.substr(i, 3)); } printf("%d\n", ans.size()); set<string>::iterator it; for (it = ans.begin(); it != ans.end(); ++it) cout << (*it) << endl; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; char ch = getchar(); bool positive = 1; for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') positive = 0; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return positive ? x : -x; } inline char RC() { char c = getchar(); while (c == ' ' || c == '\n') c = getchar(); return c; } inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } inline long long lcm(long long a, long long b, long long MOD) { return a / gcd(a, b) * b % MOD; } inline long long Sub(long long x, long long y, long long mod) { long long res = x - y; while (res < 0) res += mod; return res; } inline long long Add(long long x, long long y, long long mod) { long long res = x + y; while (res >= mod) res -= mod; return res; } inline long long POW_MOD(long long x, long long y, long long mod) { long long ret = 1; while (y > 0) { if (y & 1) ret = ret * x % mod; x = x * x % mod; y >>= 1; } return ret; } const int N = 20015; const int inf = 2100000000; const long long INF = 1LL << 62; const double PI = 3.14159265358; bool dp[N][2]; char s[N], s2[N]; set<string> ans; int main() { scanf("%s", s); int n = strlen(s); for (int i = n - 1; i >= 5; --i) { s2[n - i] = s[i]; } dp[0][0] = dp[0][1] = true; dp[2][0] = dp[3][1] = true; for (int i = 4; i <= n - 5; ++i) { dp[i][0] = dp[i - 2][1] || (dp[i - 2][0] && (s2[i] != s2[i - 2] || s2[i - 1] != s2[i - 3])); dp[i][1] = dp[i - 3][0] || (dp[i - 3][1] && (i >= 6 && (s2[i] != s2[i - 3] || s2[i - 1] != s2[i - 4] || s2[i - 2] != s2[i - 5]))); } for (int i = 2; i <= n - 5; ++i) { if (dp[i][0]) { string temp; temp += s2[i], temp += s2[i - 1]; ans.insert(temp); } if (dp[i][1]) { string temp; temp += s2[i], temp += s2[i - 1], temp += s2[i - 2]; ans.insert(temp); } } cout << ans.size() << endl; for (auto str : ans) { cout << str << endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.util.Scanner; import java.util.TreeSet; public class Ejercicio10 { static TreeSet<String> tree; static String s; static boolean[] b1; static boolean[] b2; public static void main(String[] args) { // TODO Auto-generated method stub //Reberland Linguistics String l = ""; tree = new TreeSet<String>(); Scanner sc = new Scanner(System.in); s = sc.next(); s = s.substring(5); b1 = new boolean[s.length()+1]; b2 = new boolean[s.length()+1]; reberland(s.length()-2,s.length(),l); reberland(s.length()-3,s.length(),l); System.out.println(tree.size()); for(String str:tree){ System.out.println(str); } } public static void reberland(int text,int e,String l){ if(text<0){ return; } if((b1[e]&&l.length()==2)||(b2[e]&&l.length()==3)){ return; } String sub=s.substring(text,e); if(!sub.equals(l)){ tree.add(sub); reberland(text-2,text,sub); reberland(text-3,text,sub); if(e-text==3){ b2[text]=true; }else{ b1[text]=true; } } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 10101; char s[maxn]; bool dp[maxn][4]; bool exist[30000]; inline int Hash(int l, int r) { int base = 1, ans = 0; for (int i = r; i >= l; i--) ans += ((int)s[i] - 96) * base, base *= 26; return ans; } int n; inline bool check(int i, int i2, int len) { if (i2 + len > n) return 1; for (int j = 0; j < len; j++) if (s[i + j] != s[i2 + j]) return true; return false; } set<string> res; string tmp; int main() { gets(s); n = (int)strlen(s); dp[n][2] = dp[n][3] = 1; for (int i = n - 2; i >= 5; i--) { if (dp[i + 2][3] || (check(i, i + 2, 2) && dp[i + 2][2])) { tmp = "aa"; tmp[0] = s[i], tmp[1] = s[i + 1]; res.insert(tmp); dp[i][2] = 1; } if (i < n - 2 && (dp[i + 3][2] || (check(i, i + 3, 3) && dp[i + 3][3]))) { tmp = "aaa"; tmp[0] = s[i], tmp[1] = s[i + 1], tmp[2] = s[i + 2]; res.insert(tmp); dp[i][3] = 1; } } cout << (int)res.size() << endl; while (!res.empty()) { tmp = *res.begin(); res.erase(res.begin()); cout << tmp << endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int n, can[10004]; set<string> ans; string s, cur; int main() { ios::sync_with_stdio(0); cin >> s; n = s.size(); can[n - 1] = 5; for (int r = n - 3; r >= 4; r--) { if (can[r + 2]) { if (can[r + 2] != 2 || s.substr(r + 1, 2) != s.substr(r + 3, 2)) { can[r] += 2; ans.insert(s.substr(r + 1, 2)); } } if (can[r + 3]) { if (can[r + 3] != 3 || s.substr(r + 1, 3) != s.substr(r + 4, 3)) { can[r] += 3; ans.insert(s.substr(r + 1, 3)); } } } cout << ans.size() << endl; for (string ss : ans) cout << ss << endl; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int lsone(int n) { return (n & -n); } void mult(long long int a[25][25], long long int b[25][25], long long int c[25][25], int m, int n, int p) { for (int i = 1; i <= m; i++) { for (int j = 1; j <= p; j++) { c[i][j] = 0; for (int k = 1; k <= n; k++) { c[i][j] += (a[i][k] * b[k][j]) % 1000000007; c[i][j] %= 1000000007; } } } } void mat_pow(long long int a[25][25], long long int c[25][25], int n, long long int p) { if (p == 0) { for (int i = 1; i <= n; i++) c[i][i] = 1; } else if (p == 1) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) c[i][j] = a[i][j]; } } else { long long int d[25][25]; mat_pow(a, d, n, p / 2); if (p % 2) { long long int e[25][25]; mult(d, d, e, n, n, n); mult(e, a, c, n, n, n); } else { mult(d, d, c, n, n, n); } } } long long int pow1(long long int a, long long int b) { if (b == 0) return 1ll; else if (b == 1) return a; else { long long int x = pow1(a, b / 2); x *= x; x %= 1000000007; if (b % 2) { x *= a; x %= 1000000007; } return x; } } map<string, int> m1; string s; int ans = 0, pos[4][11000]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> s; int len1 = 2; int n = s.size(); if (n <= 6) { cout << 0 << "\n"; return 0; } pos[2][n - 1] = pos[3][n - 1] = 0; pos[2][n] = 1; pos[2][n - 2] = 1; ans = 1; m1[s.substr(n - 2, 2)] = 1; for (int i = s.size() - 3; i > 4; i--) { string t1 = s.substr(i, 2); string t2 = s.substr(i, 3); if (pos[2][i + 2] or pos[3][i + 2]) { int f = 1; if (pos[2][i + 2] and !pos[3][i + 2]) { string t3 = s.substr(i + 2, 2); if (t3 == t1) f = 0; } if (f) { pos[2][i] = 1; if (m1.count(t1) < 1) { m1[t1] = 1; ans++; } } else pos[2][i] = 0; } if (pos[2][i + 3] or pos[3][i + 3]) { int f = 1; if (pos[3][i + 3] and !pos[2][i + 3]) { string t3 = s.substr(i + 3, 3); if (t3 == t2) f = 0; } if (f) { pos[3][i] = 1; if (m1.count(t2) < 1) { m1[t2] = 1; ans++; } } else pos[3][i] = 0; } } cout << ans << "\n"; for (auto elem : m1) { cout << elem.first << "\n"; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int Maxl = 10005; char str[Maxl]; int slen; bool pos[Maxl][2]; set<string> S; bool Check(int i, int len) { int j = i + len; if (j + len - 1 >= slen) return true; for (int l = 0; l < len; l++) if (str[i + l] != str[j + l]) return true; return false; } int main() { scanf("%s", str); slen = strlen(str); pos[slen][0] = pos[slen][1] = true; for (int i = slen - 1; i >= 5; i--) for (int j = 0; j < 2 && i + 2 + j - 1 < slen; j++) { bool ok = pos[i + 2 + j][1 - j]; if (Check(i, 2 + j) && pos[i + 2 + j][j]) ok = true; if (ok) { string s; for (int l = 0; l < 2 + j; l++) s += string(1, str[i + l]); S.insert(s); } pos[i][j] = ok; } printf("%d\n", S.size()); for (set<string>::iterator it = S.begin(); it != S.end(); it++) printf("%s\n", (*it).c_str()); return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
if __name__ == '__main__': word = input() suffixes = set() possible = set() my_set = set() not_poss = set() possible.add((len(word), 2)) while possible: tam, x = possible.pop() a = tam + x for i in [x, 5 - x]: root = tam - i new_pos = (root, i) if root < 5 or new_pos in my_set or (word[root:tam] == word[tam:a]): not_poss.add(word[root:tam]) else: suffixes.add(word[root:tam]) possible.add(new_pos) my_set.add(new_pos) suffixes_alph = sorted(suffixes) print(len(suffixes)) for i in suffixes_alph: print(i)
PYTHON3
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int MAX = 10005; set<string> words; bool memo[4][MAX], visit[4][MAX]; string s; int n; bool solve(int idx, int tam) { if (idx + tam == n) { string A = s.substr(idx, tam); reverse(A.begin(), A.end()); words.insert(A); return true; } if (idx + tam > n) return false; if (idx > n) return false; if (visit[tam][idx]) return memo[tam][idx]; bool &ans = memo[tam][idx]; visit[tam][idx] = true; string A = s.substr(idx, tam); reverse(A.begin(), A.end()); words.insert(A); reverse(A.begin(), A.end()); if (idx + tam + 2 > n) { ans = true; return ans; } string B = s.substr(idx + tam, 2); if (tam == 2) { if (A != B) ans |= solve(idx + tam, 2); ans |= solve(idx + tam, 3); } else { ans |= solve(idx + tam, 2); if (idx + tam + 3 <= n) { B = s.substr(idx + tam, 3); if (A != B) ans |= solve(idx + tam, 3); } } if (ans) { reverse(A.begin(), A.end()); words.insert(A); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> s; s = s.substr(5); n = (int)s.size(); reverse(s.begin(), s.end()); if (n >= 2) solve(0, 2); if (n >= 3) solve(0, 3); cout << words.size() << '\n'; for (string word : words) cout << word << '\n'; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.util.*; import java.lang.*; import java.io.*; // Created by @thesupremeone on 18/05/21 public class ReberlandLinguistics { String s; TreeSet<String> strings; Set<String> done; void explore(int i, String last){ if(i<1) return; String key = i+":"+last.length(); if(done.contains(key)) return; done.add(key); String s1 = s.charAt(i-1)+""+s.charAt(i); if(!last.equals(s1)) { strings.add(s1); explore(i-2, s1); } if(i<2) return; String s2 = s.charAt(i-2)+""+s.charAt(i-1)+""+s.charAt(i); if(!last.equals(s2)) { strings.add(s2); explore(i-3, s2); } } void solve() throws IOException { s = getLine(); if(s.length()<=5){ println(0); return; } s = s.substring(5); strings = new TreeSet<>(); done = new HashSet<>(); explore(s.length()-1, ""); println(strings.size()); for(String t : strings){ println(t); } } public static void main(String[] args) throws Exception { if (isOnlineJudge()) { in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter(System.out)); new ReberlandLinguistics().solve(); out.flush(); } else { localJudge = new Thread(); in = new BufferedReader(new FileReader("input.txt")); out = new BufferedWriter(new FileWriter("output.txt")); localJudge.start(); new ReberlandLinguistics().solve(); out.flush(); localJudge.suspend(); } } static boolean isOnlineJudge(){ try { return System.getProperty("ONLINE_JUDGE")!=null || System.getProperty("LOCAL")==null; }catch (Exception e){ return true; } } // Fast Input & Output static Thread localJudge = null; static BufferedReader in; static StringTokenizer st; static BufferedWriter out; static String getLine() throws IOException{ return in.readLine(); } static String getToken() throws IOException{ if(st==null || !st.hasMoreTokens()) st = new StringTokenizer(getLine()); return st.nextToken(); } static int getInt() throws IOException { return Integer.parseInt(getToken()); } static long getLong() throws IOException { return Long.parseLong(getToken()); } static void print(Object s) throws IOException{ out.write(String.valueOf(s)); } static void println(Object s) throws IOException{ out.write(String.valueOf(s)); out.newLine(); } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); new Main().run(in, out); out.close(); } public static long mod = 17352642619633L; static long p = 37; static long[] pPowers = new long[10004]; static { pPowers[0] = 1; for (int i = 1; i < pPowers.length; i++) { pPowers[i] = (pPowers[i-1]*p)%mod; } } void run(FastScanner in, PrintWriter out) { ca = in.next().toCharArray(); N = ca.length; canForm2 = new boolean[N]; canForm3 = new boolean[N]; go(N-1); System.out.println(s.size()); for (String ss : s) { System.out.println(ss); } } TreeSet<String> s = new TreeSet<>(); boolean[] canForm2; boolean[] canForm3; int N; char[] ca; void go(int i) { if (i <= 4) return; // can form 2 if (i+1 >= N || i+3 == N) { canForm2[i] = false; } else if (i+2 == N) { canForm2[i] = true; } else if (canForm3[i+2] || (canForm2[i+2] && (ca[i] != ca[i+2] || ca[i+1] != ca[i+3]))) { canForm2[i] = true; } // can form 3 if (i+2 >= N || (i+3 < N && i+3 >=N)) { canForm3[i] = false; } else if (i+3 == N) { canForm3[i] = true; } else if (canForm2[i+3] || (canForm3[i+3] && (ca[i] != ca[i+3] || ca[i+1] != ca[i+4] || ca[i+2] != ca[i+5]))) { canForm3[i] = true; } if (canForm2[i]) s.add(String.valueOf(ca[i]) + ca[i+1]); if (canForm3[i]) s.add(String.valueOf(ca[i]) + ca[i+1] + ca[i+2]); go(i-1); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int n = s.size(); auto get = [&](int i, int j) { string st = ""; for (int t = i; t < min(n, i + j); t++) st += s[t]; return st; }; vector<vector<bool>> can(n + 1, vector<bool>(4)); can[n][2] = can[n][3] = true; for (int i = n - 1; i >= 5; i--) { for (int j = 2; j < 4; j++) if (i + j <= n && (can[i + j][2] || can[i + j][3])) { string a = get(i, j), b = get(i + j, j); for (int k = 2; k < 4; k++) { if (a == b && j == k) continue; can[i][j] = can[i][j] | can[i + j][k]; } } } set<string> ans; for (int i = 5; i < n; i++) for (int j = 2; j < 4; j++) if (can[i][j]) ans.insert(get(i, j)); cout << ans.size() << '\n'; for (auto &i : ans) cout << i << '\n'; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; template <typename T> ostream& operator<<(ostream& out, vector<T> v) { out << '{'; for (size_t i = 0; i < v.size(); ++i) out << (i == 0 ? "" : ", ") << v[i]; return out << '}'; } template <typename Arg1> void myPrint(const char* name, Arg1&& arg1) { cerr << name << "=" << arg1 << endl; } template <typename Arg1, typename... Args> void myPrint(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << "=" << arg1; myPrint(comma, args...); } void run(); int main() { if (strlen("") > 0) { freopen( "" ".in", "r", stdin); freopen( "" ".out", "w", stdout); } run(); return 0; } int n; string s; bool dp[2][100000]; void run() { cin >> s; n = s.length(); s += "123"; dp[0][n] = dp[1][n] = 1; set<string> ans; for (int i = n - 1; i >= 5; --i) { dp[0][i] |= dp[0][i + 2] && s.substr(i, 2) != s.substr(i + 2, 2); dp[0][i] |= dp[1][i + 2]; dp[1][i] |= dp[0][i + 3]; dp[1][i] |= dp[1][i + 3] && s.substr(i, 3) != s.substr(i + 3, 3); } for (int i = 0; i < n; ++i) for (int k = 0; k < 2; ++k) if (dp[k][i]) ans.insert(s.substr(i, 2 + k)); printf("%d\n", (int)ans.size()); for (string x : ans) printf("%s\n", x.c_str()); }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.util.*; public class linguistics{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); String in = sc.nextLine(); char[] ch = in.toCharArray(); int n = ch.length; TreeSet <String> ans = new TreeSet<>(); boolean[] dp2 = new boolean[n+1]; boolean[] dp3 = new boolean[n+1]; dp2[n] = true; dp3[n] = true; dp3[n-3] = true; dp2[n-2] = true; boolean s2,s3; for (int i = n-1; i >= 5; i--) { //s2 = (ch[i-3]==ch[i-1] && ch[i-2] == ch[i]); //s3 = (ch[i-3]==ch[i] && ch[i-2] == ch[i+1] && ch[i-1] == ch[i+2]); dp2[i-3] = dp3[i-1] || (i < n && dp2[i-1] && !(ch[i-3]==ch[i-1] && ch[i-2] == ch[i])); dp3[i-3] = dp2[i] || (i+2 < n && dp3[i] && !(ch[i-3]==ch[i] && ch[i-2] == ch[i+1] && ch[i-1] == ch[i+2])); } /*for (int i = n-4; i >= 5; i--) { // s2 = ; //s3 = ; dp2[i] = dp3[i+2] || (i+3 < n && dp2[i+2] && !(ch[i]==ch[i+2] && ch[i+1] == ch[i+3])); dp3[i] = dp2[i+3] || (i+5 < n && dp3[i+3] && !(ch[i]==ch[i+3] && ch[i+1] == ch[i+4] && ch[i+2] == ch[i+5])); }*/ for (int i = 5; i < n ;i++ ) { if (dp2[i]) { ans.add("" + ch[i]+ch[i+1]); } if (dp3[i]) { ans.add(""+ch[i]+ch[i+1]+ch[i+2]); } } //Collections.sort(ans); System.out.println(""+ans.size()); for (String s: ans) { System.out.println(s); } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
t = input() s, d = set(), set() p = {(len(t), 2)} while p: m, x = p.pop() r = m + x for y in [x, 5 - x]: l = m - y q = (l, y) if q in d or l < 5 or t[l:m] == t[m:r]: continue s.add(t[l:m]) d.add(q) p.add(q) print(len(s)) print('\n'.join(sorted(s))) # Made By Mostafa_Khaled
PYTHON3
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.util.Arrays; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); s = s.substring(5); int[] allowedPath = new int[s.length()]; Arrays.fill(allowedPath, 0); Set<String> results = new TreeSet<>(); for (int i = s.length() - 2; i >= 0; i--) { int currentAP = 3; for (int size = 2; size <= 3; size++) { if (i + size == s.length()) { results.add(s.substring(i, i + size)); } else if (i + size > s.length() || allowedPath[i + size] == 0) { currentAP ^= (size - 1); } else if (allowedPath[i + size] == (size - 1)) { boolean equal = true; for (int j = 0; j < size; j++) { equal &= s.charAt(i + j) == s.charAt(i + j + size); } if (equal) { currentAP ^= (size - 1); } else { results.add(s.substring(i, i + size)); } } else { results.add(s.substring(i, i + size)); } } allowedPath[i] = currentAP; } System.out.println(results.size()); for (String r : results) { System.out.println(r); } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 5002; const int inf = 1e9; const int md = 1e9 + 7; const long long linf = 1e18; string s; int a1, a2, a3, ans; vector<string> anss; bool dp2[10001]; bool dp3[10001]; map<string, bool> used; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s; int n = s.size(); string t, nxt; dp2[n] = true; dp3[n] = true; for (int i = n - 1; i >= 0; i--) { if (i <= 4) break; if (i + 1 < n) { t = s[i]; t += s[i + 1]; dp2[i] |= (dp3[i + 2]); if (i + 3 < n) { nxt = s[i + 2]; nxt += s[i + 3]; if (nxt != t) dp2[i] |= (dp2[i + 2]); } else { dp2[i] |= (dp2[i + 2]); } if (dp2[i]) { if (!used[t]) { used[t] = true; ans++; anss.push_back(t); } } } if (i + 2 < n) { t = s[i]; t += s[i + 1]; t += s[i + 2]; dp3[i] |= (dp2[i + 3]); if (i + 5 < n) { nxt = s[i + 3]; nxt += s[i + 4]; nxt += s[i + 5]; if (nxt != t) dp3[i] |= (dp3[i + 3]); } else { dp3[i] |= (dp3[i + 3]); } if (dp3[i]) { if (!used[t]) { used[t] = true; ans++; anss.push_back(t); } } } } cout << ans << '\n'; sort(anss.begin(), anss.end()); for (int i = 0; i < ans; ++i) { cout << anss[i] << '\n'; } }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> int a, i, sz, b, x[10005][2]; char s[10005]; int u[20005]; int main() { scanf("%s", s); a = strlen(s); x[a][0] = 1; x[a][1] = 1; for (i = a - 2; i >= 5; i--) { if (x[i + 2][1] == 1 || (x[i + 2][0] == 1 && (!(s[i] == s[i + 2] && s[i + 1] == s[i + 3])))) { x[i][0] = 1; u[sz++] = (s[i] - 'a' + 1) * 27 * 27 + (s[i + 1] - 'a' + 1) * 27; } if (x[i + 3][0] == 1 || (x[i + 3][1] == 1 && (!(s[i] == s[i + 3] && s[i + 1] == s[i + 4] && s[i + 2] == s[i + 5])))) { x[i][1] = 1; u[sz++] = (s[i] - 'a' + 1) * 27 * 27 + (s[i + 1] - 'a' + 1) * 27 + s[i + 2] - 'a' + 1; } } std::sort(u, u + sz); int s = 0; for (i = 0; i < sz; i++) { if (i == 0 || u[i] != u[i - 1]) s++; } printf("%d\n", s); for (i = 0; i < sz; i++) { if (i == 0 || u[i] != u[i - 1]) { if (u[i] % 27) printf("%c%c%c", u[i] / 27 / 27 + 96, u[i] % (27 * 27) / 27 + 96, u[i] % 27 + 96); else printf("%c%c", u[i] / 27 / 27 + 96, u[i] % (27 * 27) / 27 + 96); puts(""); } } }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; bool vaild[100005 + 1000]; set<string> ans; int main(int argc, char* argv[]) { string s; cin >> s; int len = s.length(); vaild[len] = 1; for (int i = len; i > 4; i--) { if (vaild[i + 2]) { string t = s.substr(i, 2); if (s.find(t, i + 2) != i + 2 || vaild[i + 5]) { ans.insert(t); vaild[i] = 1; } } if (vaild[i + 3]) { string t = s.substr(i, 3); if (s.find(t, i + 3) != i + 3 || vaild[i + 5]) { ans.insert(t); vaild[i] = 1; } } } set<string>::iterator it; cout << ans.size() << endl; for (it = ans.begin(); it != ans.end(); it++) cout << *it << endl; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e5 + 10; const long long INF = 1e18; const long long MOD = 1e9 + 7; long long se[26][26][26], du[26][26], q[MAXN]; string v[MAXN], dp[MAXN]; int main() { string s; cin >> s; long long k = 0; q[s.size()] = 2; for (int i = s.size() - 2; i >= 5; i--) { long long a = int(s[i]) - 97, b = int(s[i + 1]) - 97, c = int(s[i + 2]) - 97; string o = s.substr(i, 3); if (q[i + 3] == 2) { if (!se[a][b][c]) { se[a][b][c] = 1; v[k] = o; k++; } q[i] = 1; dp[i] = o; } else if (q[i + 3] == 1 && o != dp[i + 3]) { if (!se[a][b][c]) { se[a][b][c] = 1; v[k] = o; k++; } q[i] = 1; dp[i] = o; } o = s.substr(i, 2); if (q[i + 2] == 2) { if (!du[a][b]) { du[a][b] = 1; v[k] = o; k++; } q[i]++; dp[i] = o; } else if (q[i + 2] == 1 && o != dp[i + 2]) { if (!du[a][b]) { du[a][b] = 1; v[k] = o; k++; } q[i]++; dp[i] = o; } } sort(v, v + k); cout << k << '\n'; for (int i = 0; i < k; i++) { cout << v[i] << '\n'; } }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; s += "###"; reverse(s.begin(), s.end()); set<string> suff; bool d[2][s.length() + 10]; for (int i = 0; i < 2; i++) { for (int j = 0; j < s.length() + 10; j++) { d[i][j] = 0; } } if (s.length() - 3 > 6) { d[0][3] = 1; } if (s.length() - 3 > 7) { d[1][3] = 1; } d[1][5] = 0; d[1][2] = 1; for (int i = 3 + 1; i < s.length(); i++) { if (i + 2 > s.length() - 5) { d[0][i] = 0; d[1][i] = 0; continue; } if (i + 3 > s.length() - 5) { d[1][i] = 0; } else { d[1][i] = d[0][i - 2] || (d[1][i - 3] and s.substr(i, 3) != s.substr(i - 3, 3)); } d[0][i] = d[1][i - 3] && i - 3 > 2 || (d[0][i - 2] and s.substr(i, 2) != s.substr(i - 2, 2)); } string f; for (int i = 3; i < s.length(); i++) { if (d[0][i]) f = s.substr(i, 2), reverse(f.begin(), f.end()), suff.insert(f); if (d[1][i]) f = s.substr(i, 3), reverse(f.begin(), f.end()), suff.insert(f); } cout << suff.size() << endl; for (string x : suff) { cout << x << endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.io.IOException; import java.io.PrintWriter; import java.util.HashSet; import java.util.Random; import java.util.Scanner; import java.util.TreeSet; public class A { private static void solve() throws IOException { char[] c = in.next().toCharArray(); boolean[] can = new boolean[c.length + 1]; can[c.length] = true; TreeSet<String> res = new TreeSet<>(); for ( int i = c.length; i >= 5; i -- ) { // System.out.println( ( i == c.length ? '.' : c[i] ) + " " + can[i] ); if ( can[i] ) { for ( int l = 2; l <= 3; l ++ ) { if ( i - l >= 5 ) { res.add( new String( c, i - l, l ) ); } boolean eq = i + l <= c.length; for ( int j = 0; j < l && i + j < c.length; j ++ ) { eq &= c[i + j] == c[i - l + j]; } if ( ! eq ) { can[i - l] = true; } else { int ll = 5 - l; can[i - l] |= i + ll <= c.length && can[i + ll]; } } } } out.println( res.size() ); for ( String s : res ) { out.println( s ); } } private static Scanner in; // private static StreamTokenizer in; private static PrintWriter out; // private static int nextInt() throws IOException { // in.nextToken(); // return ( int ) in.nval; // } public static void main( String[] args ) throws IOException { in = new Scanner( System.in ); // in = new StreamTokenizer( new InputStreamReader( System.in ) ); out = new PrintWriter( System.out ); solve(); out.close(); } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long CFMOD = 998244353; const double PI = 3.14159265358; const double EPS = 1e-8; void solve() { string s, tmp; set<string> ans; vector<bool> dp2, dp3; cin >> s; reverse(s.begin(), s.end()); dp2.resize(((int)s.size())); dp3.resize(((int)s.size())); if (((int)s.size()) >= 7) dp2[0] = true; if (((int)s.size()) >= 8) dp3[0] = true; if (((int)s.size()) >= 9 && s.substr(0, 2) != s.substr(2, 2)) dp2[2] = true; if (((int)s.size()) >= 10) dp3[2] = true; for (int i = 3; i < ((int)s.size()); i++) { if (i + 7 <= ((int)s.size())) dp2[i] = dp3[i - 3] | (dp2[i - 2] & (s.substr(i - 2, 2) != s.substr(i, 2))); if (i + 8 <= ((int)s.size())) dp3[i] = dp2[i - 2] | (dp3[i - 3] & (s.substr(i - 3, 3) != s.substr(i, 3))); } for (int i = 0; i < (((int)s.size())); i++) { if (dp2[i]) { tmp = s.substr(i, 2); reverse(tmp.begin(), tmp.end()); ans.insert(tmp); } if (dp3[i]) { tmp = s.substr(i, 3); reverse(tmp.begin(), tmp.end()); ans.insert(tmp); } } cout << ((int)ans.size()) << '\n'; for (auto i : ans) cout << i << '\n'; } int main() { int t = 1; while (t--) { solve(); } }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.util.Collections; import java.util.HashSet; import java.util.Scanner; import java.util.TreeSet; public class Linguistics { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String word = sc.nextLine(); sc.close(); int n = word.length(); if(n == 5 || n == 6) { System.out.println(0); return; } TreeSet<String> set = new TreeSet<String>(); boolean[] dp2 = new boolean[n]; boolean[] dp3 = new boolean[n]; dp2[n-2] = true; set.add(word.substring(n-2, n)); if(n > 7) { dp3[n-3] = true; set.add(word.substring(n-3, n)); } String subs; for(int i=n-4; i>=5; i--) { subs = word.substring(i, i+2); dp2[i] = dp3[i+2] || (dp2[i+2] && (!subs.equals(word.substring(i+2, i+4)))); if(dp2[i]) { set.add(subs); } subs = word.substring(i, i+3); dp3[i] = dp2[i+3] || (dp3[i+3] && (!subs.equals(word.substring(i+3, i+6)))); if(dp3[i]) { set.add(subs); } } System.out.println(set.size()); for(String s: set) { System.out.println(s); } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 1E4 + 10; char s[MAXN]; bool f[MAXN][2]; int main() { scanf("%s", s); int n = strlen(s); f[n][0] = f[n][1] = true; vector<string> lst; for (int i = n - 2; i >= 5; --i) { f[i][0] = f[i + 2][1] || f[i + 2][0] && strncmp(s + i, s + i + 2, 2); f[i][1] = f[i + 3][0] || f[i + 3][1] && strncmp(s + i, s + i + 3, 3); if (f[i][0]) lst.push_back(string(s + i, 2)); if (f[i][1]) lst.push_back(string(s + i, 3)); } sort(lst.begin(), lst.end()); lst.erase(unique(lst.begin(), lst.end()), lst.end()); printf("%d\n", (int)lst.size()); for (int i = 0; i < lst.size(); ++i) puts(lst[i].c_str()); return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4 + 5; int dp1[maxn], dp2[maxn]; set<string> my; string x; int n; int solve(int i, int l) { if (l == 2 && dp1[i] >= 0) return dp1[i]; if (l == 3 && dp2[i] >= 0) return dp2[i]; if (i + l - 1 >= n) { return false; } int left = n - (i + l - 1) - 1; if (left == 0) return true; if (left == 1) return false; if (solve(i + l, 2)) { if (l == 3) { dp2[i] = 1; return 1; } if (l == 2) { if (x.substr(i, 2) != x.substr(i + 2, 2)) { dp1[i] = 1; return 1; } } } if (solve(i + l, 3)) { if (l == 2) { dp1[i] = 1; return 1; } if (l == 3) { if (x.substr(i, 3) != x.substr(i + 3, 3)) { return dp2[i] = 1; } } } if (l == 2) return dp1[i] = 0; else return dp2[i] = 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> x; n = x.length(); memset(dp1, -1, sizeof dp1); memset(dp2, -1, sizeof dp2); for (int i = 5; i < n - 1; ++i) { if (solve(i, 2)) { my.insert(x.substr(i, 2)); } if (solve(i, 3)) { my.insert(x.substr(i, 3)); } } cout << my.size() << '\n'; for (auto s : my) cout << s << '\n'; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int dp[10010][5]; int n; string s; int solve(int pos, int lastJump) { if (dp[pos][lastJump] != -1) return dp[pos][lastJump]; if (pos >= n) return 0; if (pos == (n - 1)) { dp[pos][lastJump] = 1; return 1; } if (pos > 3) { int x = 0; if (lastJump == 0) { x = x | solve(pos + 1, 0); x = x | solve(pos + 2, 2); x = x | solve(pos + 3, 3); } else { if ((pos + 2) < n) { if (lastJump == 3 || s[pos - 1] != s[pos + 1] || s[pos] != s[pos + 2]) x = x | solve(pos + 2, 2); } if ((pos + 3) < n) { if (lastJump == 2 || s[pos - 2] != s[pos + 1] || s[pos - 1] != s[pos + 2] || s[pos] != s[pos + 3]) x = x | solve(pos + 3, 3); } } if (x == 1) { dp[pos][lastJump] = 1; return 1; } else { dp[pos][lastJump] = 0; return 0; } } int x = solve(pos + 1, lastJump); dp[pos][lastJump] = x; return x; } int main() { cin >> s; memset(dp, -1, sizeof(dp)); n = s.length(); int poss = solve(0, 0); if (poss == 0) { cout << 0 << endl; return 0; } vector<string> ans; for (int i = (n - 1); i >= 5; i--) { if (dp[i][2] == 1) { string temp; temp.push_back(s[i - 1]); temp.push_back(s[i]); ans.push_back(temp); } if (dp[i][3] == 1) { string temp; temp.push_back(s[i - 2]); temp.push_back(s[i - 1]); temp.push_back(s[i]); ans.push_back(temp); } } sort(ans.begin(), ans.end()); set<string> final; for (int i = 0; i < ans.size(); i++) { final.insert(ans[i]); } cout << final.size() << endl; for (set<string>::iterator it = final.begin(); it != final.end(); ++it) { cout << *it << endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.util.Scanner; import java.util.TreeSet; public class Competencia2 { static TreeSet<String> set = new TreeSet<>(); public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); char[] character = scan.next().toCharArray(); int n = character.length; boolean[][] dp = new boolean[n+1][2]; dp[n][0] = dp[n][1] = true; dp[n-2][0] = true; dp[n-3][1] = true; for (int i = n-4; i >= 5; i--) { dp[i][0] = dp[i+2][1] || (i+3 < n && dp[i+2][0] && !(character[i]==character[i+2] && character[i+1] == character[i+3])); dp[i][1] = dp[i+3][0] || (i+5 < n && dp[i+3][1] && !(character[i]==character[i+3] && character[i+1] == character[i+4] && character[i+2] == character[i+5])); } for (int i = 5; i < n; i++) { if (dp[i][0]) set.add("" + character[i] + character[i+1]); if (dp[i][1]) set.add("" + character[i] + character[i+1] + character[i+2]); } System.out.println(set.size()); for (String s : set) { System.out.println(s); } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; bool dp[10001][2]; int main() { string s; cin >> s; int n = s.size(); vector<string> ans; dp[n][0] = dp[n][1] = true; for (int i = n - 2; i >= 5; i--) { if (i + 2 == n) { dp[i][0] = true; string s1 = ""; s1.push_back(s[i]); s1.push_back(s[i + 1]); ans.push_back(s1); } else if (i + 3 == n) { dp[i][1] = true; string s1 = ""; s1.push_back(s[i]); s1.push_back(s[i + 1]); s1.push_back(s[i + 2]); ans.push_back(s1); } else { if ((dp[i + 2][0] && (s[i] != s[i + 2] || s[i + 1] != s[i + 3])) || (dp[i + 2][1])) { dp[i][0] = true; string s1 = ""; s1.push_back(s[i]); s1.push_back(s[i + 1]); ans.push_back(s1); } if ((dp[i + 3][1] && (s[i] != s[i + 3] || s[i + 1] != s[i + 4] || s[i + 2] != s[i + 5])) || (dp[i + 3][0])) { dp[i][1] = true; string s1 = ""; s1.push_back(s[i]); s1.push_back(s[i + 1]); s1.push_back(s[i + 2]); ans.push_back(s1); } } } sort(ans.begin(), ans.end()); ans.resize(unique(ans.begin(), ans.end()) - ans.begin()); cout << ans.size() << "\n"; for (auto s : ans) cout << s << "\n"; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:66777216") using namespace std; const int N = 10000 + 5; int n; char s[N]; int dp[N][5]; void solve() { scanf("%s", s); n = strlen(s); dp[n][0] = 1; dp[n][1] = 1; dp[n][2] = 1; dp[n][3] = 1; for (int i = (int)(n)-1; i >= 0; --i) { while (i == 5) { break; } for (int e = 2; e <= 3; ++e) { if (i + e <= n) { int j = i + e; string s1; for (int u = i; u < i + e; ++u) s1 += s[u]; if (j == n) { dp[i][e] = 1; } else { for (int e1 = 2; e1 <= 3; ++e1) { if (j + e1 <= n) { string s2; for (int u = j; u < j + e1; ++u) s2 += s[u]; if (s1 == s2) continue; if (dp[j][e1]) dp[i][e] = 1; } } } } } } set<string> se; for (int i = 5; i < n; ++i) { string c; for (int e = 1; e <= 3; ++e) { if (i + e > n) continue; c += s[i + e - 1]; if (e >= 2 && dp[i][e]) se.insert(c); } } printf("%d\n", ((int)((se).size()))); for (set<string>::iterator it = se.begin(); it != se.end(); ++it) { string c = (*it); for (int j = 0; j < (int)(((int)((c).size()))); ++j) printf("%c", c[j]); puts(""); } } void testgen() { FILE* f = fopen("input.txt", "w"); fclose(f); } int main() { cout << fixed; cout.precision(13); cerr << fixed; cerr.precision(3); solve(); return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import sys pow2 = pow # for modular expo pow2(base, n, mod) from math import * from time import time from collections import defaultdict from bisect import bisect_right, bisect_left from string import ascii_lowercase as lcs from string import ascii_uppercase as ucs from fractions import Fraction, gcd from decimal import Decimal, getcontext from itertools import product, permutations, combinations #getcontext().prec = 500 #sys.setrecursionlimit(30000) # What doesn't challenge you, makes you weaker. mod = 10**9 + 7 INF = sys.maxint raw_input = lambda: sys.stdin.readline().rstrip() die = lambda : exit(0) flush= lambda : sys.stdout.flush() r_s = lambda: raw_input().strip() #read str r_ss = lambda: raw_input().split() #read stringss r_i = lambda: int(raw_input()) #read int r_is = lambda: map(int, raw_input().split())#read ints r_f = lambda: float(raw_input()) #read float r_fs = lambda: map(Decimal, raw_input().split()) #read floats # For effieciently taking lots of queries with each query in a separate line # having integer values separated by space. A 2d list is made with each row # corresponding to line. Each row is a list with all the values. # Use this for fast I/O by taking all input in one go. r_qs = lambda: map(lambda s:map(int, s.split(' ')), sys.stdin.readlines()) '''-------------------------------------------------------------------''' def main(): s = r_s() dp2 = [0 for i in range(len(s)+1)] dp3 = [0 for i in range(len(s)+1)] if len(s)<7: print 0 return if len(s) == 7: print 1 print s[-2:] return s = '0' + s n = len(s)-1 subs = set() dp2[n-1] = 1 # print s[n-1:n+1] subs.add(s[n-1:n+1]) dp3[n-2] = 1 subs.add(s[n-2:n+1]) for i in range(n-3, 5, -1): if dp2[i+2]!=0 and s[i:i+2] != s[i+2:i+4]: dp2[i] += dp2[i+2] + 1 subs.add(s[i:i+2]) if i<n-4 and dp3[i+3]!=0 and s[i:i+3] != s[i+3:i+6]: dp3[i] += dp3[i+3] + 1 subs.add(s[i:i+3]) if dp3[i+2] != 0: subs.add(s[i:i+2]) dp2[i] += dp3[i+2] + 1 if dp2[i+3] != 0: subs.add(s[i:i+3]) dp3[i] += dp2[i+3] + 1 subs = list(subs) subs.sort() print len(subs) for elem in subs: print elem if __name__ == '__main__': try: sys.stdin = open('input.txt') print 'Running at local.' for i in range(r_i()): main() except IOError: main() # main() def dbg(var): def get_name(**kwargs): return kwargs.keys()[0] print "Value of '" + get_name(var=var) + "' is", var
PYTHON
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; long long N; char s[200005]; set<string> ans; long long hashi[200005]; bool vis[200005][2]; bool memo[200005][2]; long long pot[200005]; long long val(long long l, long long r) { long long tmp = hashi[r] - (hashi[l - 1] * pot[r - l + 1]); tmp %= 1000000007; tmp += 1000000007; tmp %= 1000000007; return tmp; } bool dp(long long l, long long f) { if (l == N + 1) return true; if (l > N) return false; if (vis[l][f]) return memo[l][f]; vis[l][f] = true; if (!f) { bool ok = false; if (l + 2 == N + 1) ok = true; else if (l + 4 <= N && dp(l + 2, 1)) ok = true; else if (l + 3 <= N && val(l, l + 1) != val(l + 2, l + 3) && dp(l + 2, 0)) ok = true; if (ok) { string x = ""; x += s[l]; x += s[l + 1]; ans.insert(x); memo[l][f] = true; } } else { bool ok = false; if (l + 3 == N + 1) ok = true; else if (l + 4 <= N && dp(l + 3, 0)) ok = true; else if (l + 5 <= N && val(l, l + 2) != val(l + 3, l + 5) && dp(l + 3, 1)) ok = true; if (ok) { string x = ""; x += s[l]; x += s[l + 1]; x += s[l + 2]; ans.insert(x); memo[l][f] = true; } } return memo[l][f]; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> s; N = strlen(s); for (long long i = N; i >= 1; i--) s[i] = s[i - 1]; pot[0] = 1; for (long long i = 1; i <= N; i++) pot[i] = (pot[i - 1] * 33) % 1000000007; for (long long i = 1; i <= N; i++) hashi[i] = ((33 * hashi[i - 1]) + (s[i] - 'a' + 1)) % 1000000007; for (long long i = 5; i <= N; i++) { if (val(1, i) == (val(i + 1, 2 * i))) continue; if (N - i < 2) continue; dp(i + 1, 0); dp(i + 1, 1); } cout << ans.size() << "\n"; for (auto v : ans) cout << v << "\n"; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 3 * 1000 * 1000 + 10; const long long mod = 1000 * 1000 * 1000 + 7; const long long inf = 1LL * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 10; long long pw(long long a, long long b) { return (b == 0 ? 1LL : ((pw(a * a % mod, b / 2) * (b & 1 == 1 ? a : 1LL)) % mod)); } long long comb(long long n, long long k) { long long _max = max(k, n - k), _min = min(k, n - k), f = 1, s = 1; for (int i = 0; i < n - _max; i++) { f *= n - i; f %= mod; } for (int i = 1; i <= _min; i++) { s *= i; s %= mod; } return ((f * pw(s, mod - 2)) % mod); } bool dp[maxn][2]; set<string> ans; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s, a, b; cin >> s; a = "aa"; b = "bbb"; dp[s.size()][0] = true; dp[s.size()][1] = true; dp[s.size() - 2][0] = true; dp[s.size() - 3][1] = true; a[0] = s[s.size() - 2]; a[1] = s[s.size() - 1]; b[0] = s[s.size() - 3]; b[1] = s[s.size() - 2]; b[2] = s[s.size() - 1]; if (s.size() >= 7) ans.insert(a); if (s.size() >= 8) ans.insert(b); for (int i = s.size() - 4; i > 4; i--) { a[0] = s[i]; a[1] = s[i + 1]; b[0] = s[i]; b[1] = s[i + 1]; b[2] = s[i + 2]; if (dp[i + 2][1] or (dp[i + 2][0] and !(s[i] == s[i + 2] and s[i + 1] == s[i + 3]))) { ans.insert(a); dp[i][0] = true; } if (dp[i + 3][0] or (dp[i + 3][1] and !(s[i] == s[i + 3] and s[i + 1] == s[i + 4] and s[i + 2] == s[i + 5]))) { ans.insert(b); dp[i][1] = true; } } cout << ans.size() << endl; for (auto it = ans.begin(); it != ans.end(); it++) cout << *it << '\n'; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int n, m, dp[1000005][2]; char a[1000005]; struct el { char a[5]; int n; } v[1000005], sol[1000005]; inline int Cmpp(el A, el B) { int i; for (i = 1; i <= A.n && i <= B.n && A.a[i] == B.a[i]; ++i) ; if (i > A.n) { if (i > B.n) return 0; return 2; } if (i > B.n) return 1; if (A.a[i] < B.a[i]) return 2; return 1; } inline bool Cmp(const el A, const el B) { return (Cmpp(A, B) == 2); } inline bool Egal(int i, int j, int len) { if (j + len - 1 > n) return 0; int k; for (k = 1; k <= len && a[i + k - 1] == a[j + k - 1]; ++k) ; return (k == len + 1); } int main() { int i, j, l = 0, p = 0; cin.sync_with_stdio(0); cin >> (a + 1); n = strlen(a + 1); dp[n - 1][0] = 1; dp[n - 2][1] = 1; for (i = n - 3; i; --i) { if (dp[i + 2][1] || (dp[i + 2][0] && !Egal(i, i + 2, 2))) dp[i][0] = 1; if (dp[i + 3][0] || (dp[i + 3][1] && !Egal(i, i + 3, 3))) dp[i][1] = 1; } for (i = 6; i < n; ++i) if (i + 2 == n + 1 || dp[i + 2][1] || (dp[i + 2][0] && !Egal(i, i + 2, 2))) { ++l; v[l].n = 2; v[l].a[1] = a[i]; v[l].a[2] = a[i + 1]; } for (i = 6; i < n; ++i) if (i + 3 == n + 1 || dp[i + 3][0] || (dp[i + 3][1] && !Egal(i, i + 3, 3))) { ++l; v[l].n = 3; v[l].a[1] = a[i]; v[l].a[2] = a[i + 1]; v[l].a[3] = a[i + 2]; } sort(v + 1, v + l + 1, Cmp); for (i = 1; i <= l; ++i) if (i == 1 || Cmpp(v[i], v[i - 1])) sol[++p] = v[i]; cout << p << "\n"; for (i = 1; i <= p; ++i) { for (j = 1; j <= sol[i].n; ++j) cout << sol[i].a[j]; cout << "\n"; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 20110; char p[N]; bool dp[N][4]; vector<string> ans; int n; string str(int u, int v) { string ans = ""; for (int i = u; i <= v; i++) ans = ans + p[i]; return ans; } int main() { scanf("%s", p + 1); n = strlen(p + 1); dp[n + 1][2] = dp[n + 1][3] = true; for (int i = n - 1; i > 5; i--) { dp[i][2] = dp[i + 2][3] || (dp[i + 2][2] && str(i, i + 1) != str(i + 2, i + 3)); dp[i][3] = dp[i + 3][2] || (dp[i + 3][3] && str(i, i + 2) != str(i + 3, i + 5)); if (dp[i][2]) ans.push_back(str(i, i + 1)); if (dp[i][3]) ans.push_back(str(i, i + 2)); } sort(ans.begin(), ans.end()); ans.resize(unique(ans.begin(), ans.end()) - ans.begin()); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
// practice with rainboy import java.io.*; import java.util.*; public class CF666A extends PrintWriter { CF666A() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF666A o = new CF666A(); o.main(); o.flush(); } void main() { byte[] cc = sc.next().getBytes(); int n = cc.length; String[] dp2 = new String[n]; String[] dp3 = new String[n]; dp2[n - 1] = new String(" "); dp3[n - 1] = new String(" "); for (int i = n - 3; i >= 4; i--) { String s = new String(cc, i + 1, 2); if (dp3[i + 2] != null || dp2[i + 2] != null && !dp2[i + 2].equals(s)) dp2[i] = s; if (i < n - 3) { s = new String(cc, i + 1, 3); if (dp2[i + 3] != null || dp3[i + 3] != null && !dp3[i + 3].equals(s)) dp3[i] = s; } } String[] ss = new String[n * 2]; int k = 0; for (int i = 4; i < n - 2; i++) { if (dp2[i] != null) ss[k++] = dp2[i]; if (i < n - 3 && dp3[i] != null) ss[k++] = dp3[i]; } Arrays.sort(ss, 0, k); int k_ = 0; for (int i = 0; i < k; i++) if (i == 0 || !ss[i].equals(ss[i - 1])) ss[k_++] = ss[i]; println(k_); for (int i = 0; i < k_; i++) println(ss[i]); } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int dp[4][N]; string str2[N], str3[N]; set<string> ans; int n; int cal(int len, int idx) { if (dp[len][idx] != -1) return dp[len][idx]; if (idx == n - 1) return dp[len][idx] = 1; ; int x = 0; if (idx + 2 < n) { if (len == 2 && str2[idx] == str2[idx + 2]) { } else x |= cal(2, idx + 2); } if (idx + 3 < n) { if (len == 3 && str3[idx] == str3[idx + 3]) { } else x |= cal(3, idx + 3); } x = x ? 1 : 0; return dp[len][idx] = x; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string s; cin >> s; n = s.length(); for (int i = 0; i < n - 1; ++i) { str2[i + 1] = s.substr(i, 2); if (i + 2 < n) str3[i + 2] = s.substr(i, 3); } for (int i = 0; i < 4; ++i) { for (int j = 0; j <= n; ++j) dp[i][j] = -1; } for (int i = 6; i < n; ++i) { if (dp[2][i] == -1) cal(2, i); if (i > 6 && dp[3][i] == -1) cal(3, i); } for (int i = 5; i < n; ++i) { if (dp[2][i] > 0) ans.insert(str2[i]); if (dp[3][i] > 0) ans.insert(str3[i]); } cout << ans.size() << endl; for (auto I : ans) cout << I << endl; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; bool dp[maxn][2]; void work() { string str; cin >> str; memset(dp, false, sizeof(dp)); int len = str.length(); str += "0"; dp[len - 3][1] = true; dp[len - 2][0] = true; for (int i = len - 4; i >= 5; --i) { if ((dp[i + 2][1] || (dp[i + 2][0] && (str[i] != str[i + 2] || str[i + 1] != str[i + 3])))) { dp[i][0] = true; } if (dp[i + 3][0] || (dp[i + 3][1] && (str[i] != str[i + 3] || str[i + 1] != str[i + 4] || str[i + 2] != str[i + 5]))) { dp[i][1] = true; } } set<string> S; for (int i = 5; i < len; ++i) { string ss; ss = ""; if (dp[i][1]) { ss += str[i]; ss += str[i + 1]; ss += str[i + 2]; S.insert(ss); } ss = ""; if (dp[i][0]) { ss += str[i]; ss += str[i + 1]; S.insert(ss); } } printf("%d\n", S.size()); set<string>::iterator it; for (it = S.begin(); it != S.end(); ++it) { cout << *it << endl; } } int main() { work(); return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; bool can2[10101]; bool can3[10101]; bool suff[32][32][32]; int main() { cin.sync_with_stdio(false); cin.tie(nullptr); string S; cin >> S; int n = S.size(); can2[n - 2] = true; can3[n - 3] = true; for (int i = n - 4; i >= 0; --i) { if (can3[i + 2] || (can2[i + 2] && (S[i] != S[i + 2] || S[i + 1] != S[i + 3]))) can2[i] = true; if (can2[i + 3] || (can3[i + 3] && (S[i] != S[i + 3] || S[i + 1] != S[i + 4] || S[i + 2] != S[i + 5]))) can3[i] = true; } for (int i = 5; i < n; ++i) { if (i + 2 <= n) { int a = S[i] - 'a' + 1; int b = S[i + 1] - 'a' + 1; if (can2[i]) suff[a][b][0] = true; if (i + 3 <= n) { int c = S[i + 2] - 'a' + 1; if (can3[i]) suff[a][b][c] = true; } } } int cnt = 0; for (int i = 0; i < 32; ++i) { for (int j = 0; j < 32; ++j) { for (int k = 0; k < 32; ++k) { cnt += (int)suff[i][j][k]; } } } cout << cnt << '\n'; for (char i = 'a'; i <= 'z'; ++i) { int a = i - 'a' + 1; for (char j = 'a'; j <= 'z'; ++j) { int b = j - 'a' + 1; if (suff[a][b][0]) cout << i << j << '\n'; for (char k = 'a'; k <= 'z'; ++k) { int c = k - 'a' + 1; if (suff[a][b][c]) cout << i << j << k << '\n'; } } } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double pi = acos(-1.0); int dblcmp(double d) { if (fabs(d) < eps) return 0; return d > eps ? 1 : -1; } int dp[111111][6]; int main() { int i, j, k; string s; cin >> s; int l = (int)((s).size()); s = "@" + s; set<string> st; memset((dp), 0, sizeof(dp)); dp[l][2] = dp[l][3] = 1; for (i = l - 1; i >= 7; i--) { if (i + 2 <= l && (l - i - 2 >= 2 || l - i - 2 == 0)) { if (dp[i + 2][3]) { dp[i][2] = 1; } string cur = s.substr(i + 1, 2); string nxt = ""; if (i + 4 <= l) nxt = s.substr(i + 3, 2); if (cur != nxt) dp[i][2] = 1; } if (i + 3 <= l && (l - i - 3 >= 2 || l - i - 3 == 0)) { if (dp[i + 3][2]) { dp[i][3] = 1; } string cur = s.substr(i + 1, 3); string nxt = ""; if (i + 6 <= l) nxt = s.substr(i + 4, 3); if (cur != nxt) dp[i][3] = 1; } } for (i = 7; i <= l; i++) { if ((!dp[i][3]) && (!dp[i][2])) continue; if (i >= 7) { string cur = s.substr(i - 1, 2); if (dp[i][3]) { st.insert(cur); } string nxt = ""; if (i + 2 <= l) nxt = s.substr(i + 1, 2); if (cur != nxt) st.insert(cur); } if (i >= 8) { string cur = s.substr(i - 2, 3); if (dp[i][2]) { st.insert(cur); } string nxt = ""; if (i + 3 <= l) nxt = s.substr(i + 1, 3); if (cur != nxt) st.insert(cur); } } cout << (int)((st).size()) << endl; for (__typeof(st.begin()) e = st.begin(); e != st.end(); ++e) cout << *e << endl; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; string s; int l; int vis[10009][4]; set<string> suff; bool solve(int idx, string prev) { if (idx == l) return 1; if (idx > l) return 0; int z = prev.length(); if (vis[idx][z]) return vis[idx][z]; bool flg1 = 0, flg2 = 0; if (idx + 2 <= l) { string s1; for (int i = idx; i < idx + 2; i++) { s1 += s[i]; } if (s1 != prev) { flg1 = solve(idx + 2, s1); } if (flg1 == 1) { suff.insert(s1); } } if (idx + 3 <= l) { string s1; for (int i = idx; i < idx + 3; i++) { s1 += s[i]; } if (s1 != prev) { flg2 = solve(idx + 3, s1); } if (flg2 == 1) { suff.insert(s1); } } return (vis[idx][z] = (flg1 || flg2)); } int main() { cin >> s; l = s.length(); for (int i = 5; i < s.length(); i++) { solve(i, ""); } cout << suff.size() << endl; for (auto it : suff) cout << it << endl; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int main() { char s[10001]; scanf("%s", s); string str(s); int n = (int)str.length(); vector<set<string>> answer(n); for (auto &it : answer) it.clear(); if (str.length() <= 6) cout << "0" << endl; else if (str.length() == 7) cout << "1\n" << str.substr(n - 2, 2) << endl; else { answer[n - 2].insert(str.substr(n - 2, 2)); answer[n - 3].insert(str.substr(n - 3, 3)); for (int i = n - 4; i >= 5; i--) { string str2 = str.substr(i, 2), str3 = str.substr(i, 3); if (!answer[i + 2].empty() && !((answer[i + 2].size() == 1) && (*answer[i + 2].begin() == str2))) answer[i].insert(str2); if ((!answer[i + 3].empty()) && !((answer[i + 3].size() == 1) && (*answer[i + 3].begin() == str3))) answer[i].insert(str3); } set<string> final; for (auto st : answer) { for (auto it : st) { final.insert(it); } } cout << final.size() << endl; for (auto it : final) cout << it << endl; } }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int MAX = 1e4 + 10; bool dp[MAX][2]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; int n = s.size(); dp[n - 2][0] = dp[n - 3][1] = true; set<string> ans; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 2; j++) { if (i + j + 2 > n) continue; if (dp[i + j + 2][j ^ 1]) dp[i][j] = true; else if (dp[i + j + 2][j] && s.substr(i, j + 2) != s.substr(i + j + 2, j + 2)) dp[i][j] = true; if (i >= 5 && dp[i][j]) ans.insert(s.substr(i, j + 2)); } } cout << ans.size() << "\n"; for (auto c : ans) cout << c << "\n"; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; string str; set<string> s; map<pair<string, int>, int> dp; int func(string tmp, int n) { if (n >= 4 && tmp.length() != 0) { s.insert(tmp); } if (n <= 4) { return 0; } if (dp[make_pair(tmp, n)] != 0) { return dp[make_pair(tmp, n)]; } string t1 = "", t2 = ""; int ans = 0; t1 = t1 + str[n - 1] + str[n]; t2 = t2 + str[n - 2] + str[n - 1] + str[n]; if (tmp != t1 && n - 2 >= 4) { ans += 1 + func(t1, n - 2); } if (tmp != t2 && n - 3 >= 4) { ans += 1 + func(t2, n - 3); } return dp[make_pair(tmp, n)] = ans; } int main() { cin >> str; func("", str.length() - 1); cout << s.size() << endl; set<string>::iterator it; for (it = s.begin(); it != s.end(); it++) { cout << *it << endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int n, dp[10007][4]; set<string> S; string str; int rec(int pos, int prev, string back) { if (pos == n && prev != n - 1) return 1; int &ret = dp[pos][pos - prev]; if (ret != -1) return ret; int now1 = 0, now2 = 0, now3 = 0; string st; if (pos + 2 <= n) { st += str[pos]; st += str[pos + 1]; if (st != back) now1 = now1 | rec(pos + 2, pos, st); if (now1) S.insert(st); } if (pos + 3 <= n) { st += str[pos + 2]; if (st != back) now2 = now2 | rec(pos + 3, pos, st); if (now2) S.insert(st); } rec(pos + 1, pos + 1, ""); return ret = max(now1, now2); } int main(int argc, char const *argv[]) { cin >> str; memset(dp, -1, sizeof dp); n = str.length(); if (n < 7) { cout << 0 << endl; return 0; } for (int i = 5; i < 6; i++) { rec(i, i, ""); } cout << S.size() << endl; set<string>::iterator it; for (it = S.begin(); it != S.end(); it++) cout << *it << endl; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; inline int in() { int32_t x; scanf("%d", &x); return x; } inline string get() { char ch[100010]; scanf("%s", ch); return ch; } const int MAX_LG = 21; const long long maxn = 1e5 + 10; const long long base = 29; const long long mod = 1e9 + 7; bool dp2[maxn], dp3[maxn]; set<string> st; int32_t main() { string s = get(); long long sz = (long long)s.size(); dp2[sz - 2] = dp3[sz - 3] = true; dp2[sz] = dp3[sz] = true; for (long long i = s.size() - 4; i >= 0; i--) { if (s.substr(i, 2) != s.substr(i + 2, 2)) dp2[i] |= dp2[i + 2]; dp2[i] |= dp3[i + 2], dp3[i] |= dp2[i + 3]; if (i < s.size() - 5) if (s.substr(i, 3) != s.substr(i + 3, 3)) dp3[i] |= dp3[i + 3]; } for (long long i = 5; i + 1 < s.size(); i++) { string t = ""; t += s[i]; for (long long j = i + 1; j < min((long long)s.size(), i + 3); j++) { t += s[j]; long long ln = j - i + 1; if ((ln == 2 && dp2[i]) || (ln == 3 && dp3[i])) st.insert(t); } } cout << st.size() << "\n"; for (auto v : st) cout << v << "\n"; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x); template <typename T> void write(T x); template <typename T> void writesp(T x); template <typename T> void writeln(T x); const long long N = 1e5 + 5; char wn[N]; long long n; bool f[N][2]; bool vis[N][2]; string s[N]; long long cnt; inline bool dfs(long long now, long long st) { if (now > n) return false; if (vis[now][st]) return f[now][st]; if (now == n - 1) return false; if (now == n - 2 && st == 3) return false; if (now == n) return f[now][st] = true; vis[now][st] = true; if (st == 0) { if (wn[now + 1] != wn[now - 1] || wn[now + 2] != wn[now]) f[now][st] |= dfs(now + 2, 0); f[now][st] |= dfs(now + 3, 1); } else if (st == 1) { if (wn[now + 1] != wn[now - 2] || wn[now + 2] != wn[now - 1] || wn[now + 3] != wn[now]) f[now][st] |= dfs(now + 3, 1); f[now][st] |= dfs(now + 2, 0); } return f[now][st]; } int main() { scanf("%s", wn + 1); n = strlen(wn + 1); if (n <= 6) return writeln(0), 0; dfs(7, 0); for (register long long i = n; i >= 8; i--) dfs(i, 0), dfs(i, 1); for (register long long i = 7; i <= n; i++) { if (f[i][0]) { ++cnt; ((s[cnt] = "") += wn[i - 1]) += wn[i]; } if (f[i][1]) { ++cnt; (((s[cnt] = "") += wn[i - 2]) += wn[i - 1]) += wn[i]; } } sort(s + 1, s + cnt + 1); cnt = unique(s + 1, s + cnt + 1) - s - 1; writeln(cnt); for (register long long i = 1; i <= cnt; i++) cout << s[i] << endl; } template <typename T> void read(T &x) { x = 0; int t = 1; char wn = getchar(); while (wn < '0' || wn > '9') { if (wn == '-') t = -1; wn = getchar(); } while (wn >= '0' && wn <= '9') { x = x * 10 + wn - '0'; wn = getchar(); } x *= t; } template <typename T> void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x <= 9) { putchar(x + '0'); return; } write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writesp(T x) { write(x); putchar(' '); } template <typename T> void writeln(T x) { write(x); putchar('\n'); }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; set<string> result; string s; int n; int main() { ios::sync_with_stdio(NULL); cin.tie(NULL); cin >> s; n = int(s.size()); vector<bool> can3(n + 3, false); vector<bool> can2(n + 3, false); can2[n] = can3[n] = true; for (int i = n - 1; i >= 0; i--) { if (can2[i + 3] || (can3[i + 3] && s.substr(i, 3) != s.substr(i + 3, 3))) can3[i] = true; if (can3[i + 2] || (can2[i + 2] && s.substr(i, 2) != s.substr(i + 2, 2))) can2[i] = true; } for (int i = 5; i < n; ++i) { if (can2[i]) result.insert(s.substr(i, 2)); if (can3[i]) result.insert(s.substr(i, 3)); } cout << int(result.size()) << '\n'; for (auto to : result) cout << to << '\n'; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int dp[4][10100]; char str[10100]; int n; set<string> S; int get(int bad, int pos) { if (pos == n) return 1; if (pos > n) return 0; if (dp[bad][pos] + 1) return dp[bad][pos]; int r = 0; if (bad != 2) { int u = 1; if (str[pos] != str[pos + 2] || str[pos + 1] != str[pos + 3]) u = 0; if (get(u * 2, pos + 2)) { string p; p.push_back(str[pos]); p.push_back(str[pos + 1]); S.insert(p); ; r = 1; } } if (bad != 3) { int u = 1; if (str[pos] != str[pos + 3] || str[pos + 1] != str[pos + 4] || str[pos + 2] != str[pos + 5]) u = 0; if (get(u * 3, pos + 3)) { string p; p.push_back(str[pos]); p.push_back(str[pos + 1]); p.push_back(str[pos + 2]); ; S.insert(p); r = 1; } }; return dp[bad][pos] = r; } int main() { scanf(" %s", str); n = strlen(str); memset(dp, -1, sizeof(dp)); for (int i = 5; i < n; i++) get(0, i); printf("%d\n", (int)S.size()); for (set<string>::iterator it = S.begin(); it != S.end(); it++) cout << *it << endl; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.util.Scanner; import java.util.TreeSet; public class ReberlandLinguistics3 { static String s; static int n; static boolean valid(int pos, int chars) { if(pos - chars < 0)return true; for(int i = pos-chars+1; i <= pos; i++) { if(s.charAt(i) != s.charAt(i-chars)) return true; } return false; } public static void main(String[] args) { TreeSet<String> set = new TreeSet<String>(); Scanner sc = new Scanner(System.in); s = sc.next(); n = s.length(); boolean[] wrong2 = new boolean[n]; boolean[] wrong3 = new boolean[n]; boolean[] valid = new boolean[n]; //validated from 2 boolean[] did2 = new boolean[n]; boolean[] did3 = new boolean[n]; valid[n-1] = true; boolean debug = false; for(int i = n-1; i >= 5; i--) { if(debug)System.out.println("i " + i); if(!valid[i])continue; //try 2 if(i >= 6) { if(!wrong2[i] || ((i+3 <= n-1) && did3[i+3]) ) { if(i-2>0)valid[i-2] = true; set.add(s.substring(i-1, i+1)); if(debug)System.out.println("ADD " + s.substring(i-1, i+1) + ", " + (i-2) + " valid2"); did2[i] = true; if(valid(i, 2)) { //add 2 chars from i } else { wrong2[i-2] = true; } } } //try 3 if(i >= 7) { if(!wrong3[i] || ((i+2 <= n-1) && did2[i+2]) ) { if(i-3>0)valid[i-3] = true; set.add(s.substring(i-2, i+1)); if(debug) { System.out.println("ADD " + s.substring(i-2, i+1) + ", " + (i-3) + " valid3"); if(i+2<=n-1)System.out.println(" EX " + did2[i+2] + ", wrong " + wrong3[i]); } did3[i] = true; if(valid(i, 3)) { //add 3 chars from i } else { wrong3[i-3] = true; } } } } StringBuilder sb = new StringBuilder(); sb.append(set.size()+"\n"); for(String ss : set) { sb.append(ss + "\n"); } System.out.print(sb); } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.io.*; import java.util.*; public class R349qA { public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); char s[] = in.readString().toCharArray(); TreeSet<String> ans = new TreeSet<String>(); int n = s.length; boolean dp2[] = new boolean[n + 1]; dp2[n] = true; boolean dp3[] = new boolean[n + 1]; dp3[n] = true; for(int i = s.length - 1; i >= 5; i--){ if(i + 1 < s.length){ boolean c = dp3[i + 2]; if(dp2[i + 2]){ if(i + 2 == n) c |= true; else{ if(s[i + 2] != s[i] || s[i + 3] != s[i + 1]) c |= true; } } dp2[i] = c; if(dp2[i]){ String x = new String(); x = x + s[i]; x = x + s[i + 1]; ans.add(x); } } if(i + 2 < s.length){ boolean c = dp2[i + 3]; if(dp3[i + 3]){ if(i + 3 == n) c |= true; else{ if(s[i + 3] != s[i] || s[i + 4] != s[i + 1] || s[i + 5] != s[i + 2]) c |= true; } } dp3[i] = c; if(dp3[i]){ String x = new String(); x = x + s[i]; x = x + s[i + 1]; x = x + s[i + 2]; ans.add(x); } } } w.println(ans.size()); for(String x : ans) w.println(x); w.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, const U &b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, const U &b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) { gn(x1), gn(x2); } template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) { gn(x1, x2), gn(x3); } template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) { gn(x1, x2, x3), gn(x4); } template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { gn(x1, x2, x3, x4), gn(x5); } template <class T> inline void print(T first) { if (first < 0) { putchar('-'); return print(-first); } if (first < 10) { putchar('0' + first); return; } print(first / 10); putchar(first % 10 + '0'); } template <class T> inline void println(T first) { print(first); putchar('\n'); } template <class T> inline void printsp(T first) { print(first); putchar(' '); } template <class T1, class T2> inline void print(T1 x1, T2 x2) { printsp(x1), println(x2); } template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) { printsp(x1), printsp(x2), println(x3); } template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) { printsp(x1), printsp(x2), printsp(x3), println(x4); } template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) { printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } char s[10010]; char a[10010]; int dp[10010][10]; vector<string> ans; string t; int calc_two(int u) { if (u == 1) return 0; if (u == 2) return 1; if (dp[u - 2][2]) { if (a[u - 3] != a[u - 1] || a[u - 2] != a[u]) return 1; } if (dp[u - 2][3]) return 1; return 0; } int calc_three(int u) { if (u < 3) return 0; if (u == 3) return 1; if (dp[u - 3][2]) return 1; if (dp[u - 3][3]) { if (a[u - 5] != a[u - 2] || a[u - 4] != a[u - 1] || a[u - 3] != a[u]) return 1; } return 0; } int main() { scanf("%s", s); int n = strlen(s); if (n <= 6) { puts("0"); return 0; } for (int i = 1; i < n - 4; i++) a[i] = s[i + 4]; n -= 5; reverse(a + 1, a + n + 1); dp[0][2] = dp[0][3] = 1; for (int i = 1; i <= n; i++) { dp[i][2] = calc_two(i); dp[i][3] = calc_three(i); } for (int i = 1; i <= n; i++) { if (dp[i][2]) { t.clear(); t += a[i]; t += a[i - 1]; ans.push_back(t); } if (dp[i][3]) { t.clear(); t += a[i]; t += a[i - 1]; t += a[i - 2]; ans.push_back(t); } } sort(ans.begin(), ans.end()); ans.resize(unique(ans.begin(), ans.end()) - ans.begin()); println(ans.size()); for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 1e4 + 10; int n, f[N][2]; int v1[30][30], v2[30][30][30]; char s[N]; vector<string> res; int main() { scanf("%s", s + 1); n = strlen(s + 1); if (n >= 7) f[n][0] = 1; if (n >= 8) f[n][1] = 1; for (int i = n; i >= 7; i--) { if (f[i][0]) { v1[s[i - 1] - 'a'][s[i] - 'a'] = 1; if (i - 2 >= 8) f[i - 2][1] = 1; if (i - 2 >= 7 && (s[i - 3] != s[i - 1] || s[i - 2] != s[i])) f[i - 2][0] = 1; } if (f[i][1]) { v2[s[i - 2] - 'a'][s[i - 1] - 'a'][s[i] - 'a'] = 1; if (i - 3 >= 7) f[i - 3][0] = 1; if (i - 3 >= 8 && (s[i - 5] != s[i - 2] || s[i - 4] != s[i - 1] || s[i - 3] != s[i])) f[i - 3][1] = 1; } } for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if (v1[i][j]) { string tmp; tmp += i + 'a'; tmp += j + 'a'; res.push_back(tmp); } for (int k = 0; k < 26; k++) { if (v2[i][j][k]) { string tmp; tmp += i + 'a'; tmp += j + 'a'; tmp += k + 'a'; res.push_back(tmp); } } } } printf("%d\n", res.size()); sort(res.begin(), res.end()); for (string str : res) { cout << str << endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int n; char a[20000]; int d[20000][2]; int main() { int i, j, k, l; scanf("%s", a); n = strlen(a); d[n][0] = d[n][1] = 1; for (i = n - 2; i >= 5; i--) { d[i][0] |= d[i + 2][1]; if (make_pair(a[i], a[i + 1]) != make_pair(a[i + 2], a[i + 3])) d[i][0] |= d[i + 2][0]; if (i < n - 2) { d[i][1] |= d[i + 3][0]; if (make_pair(make_pair(a[i], a[i + 1]), a[i + 2]) != make_pair(make_pair(a[i + 3], a[i + 4]), a[i + 5])) d[i][1] |= d[i + 3][1]; } } set<pair<pair<char, char>, char>> s; for (i = 5; i <= n - 2; i++) { if (d[i][0]) s.emplace(make_pair(a[i], a[i + 1]), 0); if (d[i][1]) s.emplace(make_pair(a[i], a[i + 1]), a[i + 2]); } cout << s.size() << endl; for (auto e : s) { printf("%c%c", e.first.first, e.first.second); if (e.second) printf("%c", e.second); printf("\n"); } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
s = raw_input() if len(s)<7: print 0 elif len(s) == 7: print 1 print s[-2:] else: dp2 = [False for i in range(len(s)+1)] # dp2[i] = 1 means its possible to pick s[i:i+2] dp3 = [False for i in range(len(s)+1)] # dp3[i] = 1 means its possible to pick s[i:i+3] n = len(s) s = '0' + s # For 1 - indexing subs = set() dp2[n-1] = True subs.add(s[n-1:n+1]) dp3[n-2] = True subs.add(s[n-2:n+1]) for i in range(n-3, 5, -1): if (dp2[i+2] and s[i:i+2] != s[i+2:i+4]) or dp3[i+2]: dp2[i] = True subs.add(s[i:i+2]) if (dp3[i+3] and s[i:i+3] != s[i+3:i+6]) or dp2[i+3]: dp3[i] = True subs.add(s[i:i+3]) subs = sorted(list(subs)) print len(subs) for elem in subs: print elem
PYTHON
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.util.Scanner; import java.util.TreeSet; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); TreeSet<String> suffixes = new TreeSet<>(); String word = sc.nextLine(); sc.close(); String tempSubstring; boolean doubleStepIsPossible[] = new boolean[word.length()]; boolean tripleStepIsPossible[] = new boolean[word.length()]; if(word.length() <= 6){ System.out.println("0"); return; } int wordLength = word.length(); doubleStepIsPossible[wordLength - 2] = true; suffixes.add(word.substring(wordLength - 2, wordLength)); if(wordLength > 7){ tripleStepIsPossible[wordLength - 3] = true; suffixes.add(word.substring(wordLength - 3, wordLength)); } for(int i = wordLength - 4; i >= 5; i--) { tempSubstring = word.substring(i, i + 2); if(tripleStepIsPossible[i + 2] || (doubleStepIsPossible[i + 2] && (!tempSubstring.equals(word.substring(i + 2, i + 4))))){ doubleStepIsPossible[i] = true; suffixes.add(tempSubstring); } tempSubstring = word.substring(i, i + 3); if(doubleStepIsPossible[i + 3] || (tripleStepIsPossible[i + 3] && (!tempSubstring.equals(word.substring(i + 3, i + 6))))){ tripleStepIsPossible[i] = true; suffixes.add(tempSubstring); } } System.out.println(suffixes.size()); suffixes.forEach(System.out::println); } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int to[10100], ant[10100], adj[6010], z; const int oo = 0x3f3f3f3f; char str[100100]; bool can2[100100], can3[100100]; char tmp[10]; set<string> ans; void addStr(int a, int b) { for (int(i) = (a); (i) < (b + 1); ++(i)) { tmp[i - a] = str[i]; } tmp[b - a + 1] = 0; ans.insert(tmp); } int main() { ans.clear(); scanf("%s", str); int n = strlen(str); reverse(str, str + n); memset((can2), (0), sizeof(can2)); memset((can3), (0), sizeof(can3)); for (int(i) = (1); (i) < (n - 5); ++(i)) { if (i == 1) { addStr(i - 1, i); can2[i] = 1; } else if (i == 2) { addStr(i - 2, i); can3[i] = 1; } else { if ((strncmp(str + i - 3, str + i - 1, 2) != 0 && can2[i - 2]) || can3[i - 2]) { addStr(i - 1, i); can2[i] = 1; } if ((i >= 5 && strncmp(str + i - 5, str + i - 2, 3) != 0 && can3[i - 3]) || can2[i - 3]) { addStr(i - 2, i); can3[i] = 1; } } } printf("%d\n", (int)ans.size()); set<string> ans2; for (auto s : ans) { reverse(s.begin(), s.end()); ans2.insert(s); } for (auto s : ans2) { printf("%s\n", s.c_str()); } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
string = input() s = set() finded = {} import sys sys.setrecursionlimit(900000000) def get_substr(string, start, parent): # Π±Π°Π·ΠΎΠ²Ρ‹ΠΉ случай if start >= len(string): return if start+1 < len(string): substr = string[start:start+2] # ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΠΌ Π½Π΅ Ρ‚Π° ΠΆΠ΅ Π»ΠΈ это строка if substr != parent: s.add(substr) if (start+2, 2) not in finded: get_substr(string, start+2, substr) finded[(start+2, 2)] = True if start+2 < len(string): substr = string[start:start+3] # ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΠΌ Π½Π΅ Ρ‚Π° ΠΆΠ΅ Π»ΠΈ это строка if substr != parent: s.add(substr) if (start+3, 3) not in finded: get_substr(string, start+3, substr) finded[(start+3, 3)] = True get_substr(string[5:][::-1], 0, "") print(len(s)) ans = [] for i in s: ans.append(i[::-1]) for a in sorted(ans): print(a)
PYTHON3
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; string S; vector<string> V; int n, d[10005][3], ans; void dp(int x, int len) { if (d[x][len] != 0) return; d[x][len] = 1; string s2(S, x - 1, 2), s3(S, x - 2, 3); if (len == 2) { if (x >= 6) { V.push_back(s2); dp(x - 2, 0); } if (x >= 7) { V.push_back(s3); dp(x - 3, 1); } return; } string st(S, x + 1, len + 2); if (x >= 6 && st != s2) { V.push_back(s2); dp(x - 2, 0); } if (x >= 7 && st != s3) { V.push_back(s3); dp(x - 3, 1); } return; } int main() { cin >> S; n = S.length(); dp(n - 1, 2); sort(V.begin(), V.end()); for (int i = 0; i < (int)V.size(); i++) { string t; if (i != 0) t = V[i - 1]; if (V[i] != t) ans++; } cout << ans << endl; for (int i = 0; i < (int)V.size(); i++) { string t; if (i != 0) t = V[i - 1]; if (V[i] != t) cout << V[i] << endl; } }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; const int N = 1e4; char s[N + 3]; bool v[2][N + 3]; set<string> ans; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed; cin >> s; int n = strlen(s); v[0][n] = v[1][n] = true; for (int i = n - 2; i > 4; i--) { for (int l = 2; l <= 3; l++) { int i0 = i + l; if ((v[l - 2][i0] && strncmp(s + i, s + i0, l) != 0) || v[5 - l - 2][i0]) { v[l - 2][i] = true; ans.emplace(s + i, l); }; } } cout << ans.size() << endl; for (auto& ss : ans) cout << ss << '\n'; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int maxN = 10005, MOD = 1e7 + 7, eps = 1e-7; int dp2[maxN], dp3[maxN]; string str; string range(int l, int step) { return str.substr(l, step); } set<string> S; bool check2(int n) { if (n < 3) { return true; } return range(n - 3, 2) != range(n - 1, 2); } bool check3(int n) { if (n < 5) { return true; } return range(n - 5, 3) != range(n - 2, 3); } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> str; int n = str.length(); reverse((str).begin(), (str).end()); if (n > 6) { dp2[1] = 1; if (n > 7) dp3[2] = 1; } for (int i = int(2); i < int(n - 5); i++) { if (dp3[max(0, i - 2)]) { dp2[i] = 1; } else if (i > 2) { dp2[i] = check2(i) && dp2[i - 2]; } else { dp2[i] = 0; } if (i == 2) dp3[i] = 1; else if (dp2[max(0, i - 3)]) { dp3[i] = 1; } else if (i > 4) { dp3[i] = check3(i) && dp3[i - 3]; } else { dp3[i] = 0; } } for (int i = int(0); i < int(n); i++) { if (dp2[i]) { string aux = range(i - 1, 2); reverse((aux).begin(), (aux).end()); S.insert(aux); } if (dp3[i]) { string aux = range(i - 2, 3); reverse((aux).begin(), (aux).end()); S.insert(aux); } } cout << S.size() << '\n'; for (string s : S) { cout << s << '\n'; } }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package CP; import java.io.*; import java.util.*; public class A666 { static TreeSet<String> set;static String s; static boolean b1[],b2[]; static void solve(int st,int end,String str) { if(st<0){ return; } if((b1[end]&&str.length()==2)||(b2[end]&&str.length()==3)){ return; } String sub=s.substring(st,end); if(!sub.equals(str)){ set.add(sub); solve(st-2,st,sub); solve(st-3,st,sub); if(end-st==3){ b2[st]=true; }else{ b1[st]=true; } } } public static void main(String args[])throws IOException { Scanner sc=new Scanner(System.in); s=sc.next(); String l = ""; set = new TreeSet<String>(); //Scanner sc = new Scanner(System.in); //s = sc.next(); s = s.substring(5); b1 = new boolean[s.length()+1]; b2 = new boolean[s.length()+1]; solve(s.length()-2,s.length(),l); solve(s.length()-3,s.length(),l); System.out.println(set.size()); for(String str:set) { System.out.println(str); } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; string s; bool dp[N]; int main() { while (cin >> s) { int n = s.length(); set<string> se; for (int i = 0; i <= n; i++) dp[i] = true; dp[n - 1] = false; for (int i = n - 2; i >= 5; i--) { int cnt = 0; if (i + 2 <= n && dp[i + 2]) { string now = s.substr(i, 2); if (now != s.substr(i + 2, 2) || dp[i + 5]) { se.insert(now); cnt++; } } if (i + 3 <= n && dp[i + 3]) { string now = s.substr(i, 3); if (now != s.substr(i + 3, 3) || dp[i + 5]) { se.insert(now); cnt++; } } if (cnt == 0) dp[i] = false; } cout << (int)se.size() << endl; for (set<string>::iterator it = se.begin(); it != se.end(); it++) { cout << *it << endl; } } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int cmx = 1e4 + 5; int n; string s; int dp[cmx][2]; set<string> ans; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; n = s.size(); if (n <= 6) { cout << 0 << '\n'; return 0; } dp[n - 1][1] = 1; if (n > 7) dp[n - 1][2] = 1; for (int i = n - 1; i > 5; i--) { if (i - 4 >= 5) { dp[i - 2][2] = dp[i - 2][2] or dp[i][1]; dp[i - 3][1] = dp[i - 3][1] or dp[i][2]; } if (i - 3 >= 5 and s.substr(i - 3, 2) != s.substr(i - 1, 2)) { dp[i - 2][1] = (dp[i - 2][1] or dp[i][1]); } if (i - 5 >= 5 and s.substr(i - 5, 3) != s.substr(i - 2, 3)) { dp[i - 3][2] = (dp[i - 3][2] or dp[i][2]); } } for (int i = 6; i < n; i++) { if (dp[i][1]) ans.insert(s.substr(i - 1, 2)); if (dp[i][2]) ans.insert(s.substr(i - 2, 3)); } cout << ans.size() << '\n'; for (auto x : ans) cout << x << '\n'; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.io.*; import java.lang.*; import java.util.*; // Sachin_2961 submission // public class CodeforcesA { static TreeSet<String>treeSet = new TreeSet<>(); public void solve() { char[]str = fs.next().toCharArray(); int n = str.length; boolean[][]dp = new boolean[n+1][2]; dp[n][0] = dp[n][1] = true; dp[n-2][0] = true; dp[n-3][1] = true; for(int i=n-4;i>=5;i--){ dp[i][0] = dp[i+2][1] || ( i+3 < n && dp[i+2][0] && !(str[i] == str[i+2] && str[i+1] == str[i+3]) ); dp[i][1] = dp[i+3][0] || ( i+5 < n && dp[i+3][1] && !(str[i] == str[i+3] && str[i+1] == str[i+4] && str[i+2] == str[i+5])); } for(int i=5;i<n;i++){ if(dp[i][0]){ treeSet.add(""+str[i]+str[i+1]); } if(dp[i][1]){ treeSet.add(""+str[i]+str[i+1]+str[i+2]); } } out.println(treeSet.size()); for(String s:treeSet) out.println(s); } static boolean multipleTestCase = false; static FastScanner fs; static PrintWriter out; public void run(){ fs = new FastScanner(); out = new PrintWriter(System.out); int tc = (multipleTestCase)?fs.nextInt():1; while (tc-->0)solve(); out.flush(); out.close(); } public static void main(String[]args){ try{ new CodeforcesA().run(); }catch (Exception e){ e.printStackTrace(); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeSet; public class Solution{ static final int N = (int)1e5+10; static final long inf = (long)1e18; public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tt = 1; while(tt-->0) { String s = fs.next(); StringBuilder str = new StringBuilder(s.substring(5, s.length())); str.reverse(); int n = str.length(); TreeSet<String> set = new TreeSet<String>(); boolean[] dp2 = new boolean[n]; boolean[] dp3 = new boolean[n]; if(n>=2) { dp2[1] = true; set.add(rev(str.substring(0, 2))); } if(n>=3) { dp3[2] = true; set.add(rev(str.substring(0, 3))); } for(int i=3;i<n;i++) { dp2[i] = dp3[i-2] || (dp2[i-2] && !str.substring(i-3, i-1).equals(str.substring(i-1, i+1))); dp3[i] = dp2[i-3] || (dp3[i-3] && !str.substring(i-5, i-2).equals(str.substring(i-2, i+1))); if(dp2[i]) set.add(rev(str.substring(i-1, i+1))); if(dp3[i]) set.add(rev(str.substring(i-2, i+1))); int x = 1; } out.println(set.size()); for(String st: set) out.println(st); } out.close(); } static String rev(String str) { StringBuilder st = new StringBuilder(str); return st.reverse().toString(); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong() { return Long.parseLong(next()); } public char nextChar() { return next().toCharArray()[0]; } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; bool valid[10000 + 100]; set<string> ans; int main() { string s; cin >> s; valid[s.length()] = 1; for (int i = s.length() - 1; i > 4; --i) { if (valid[i + 2]) { string t = s.substr(i, 2); if (s.find(t, i + 2) != i + 2 || valid[i + 5]) { valid[i] = 1; ans.insert(t); } } if (valid[i + 3]) { string t = s.substr(i, 3); if (s.find(t, i + 3) != i + 3 || valid[i + 5]) { valid[i] = 1; ans.insert(t); } } } cout << ans.size() << "\n"; for (auto &ite : ans) { cout << ite << "\n"; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; int n, l; set<string> ans; string s; map<pair<int, string>, int> mp; bool solve(int idx, string tmp = "") { if (idx == s.size()) { return 1; } if (idx > s.size()) return 0; if (mp.count({idx, tmp})) { return mp[{idx, tmp}]; } string s3, s2; bool x3, x2; x3 = x2 = 0; if (idx + 2 <= s.size()) { s2 = s.substr(idx, 2); if (s2 != tmp) { x2 = solve(idx + 2, s2); } } if (idx + 3 <= s.size()) { s3 = s.substr(idx, 3); if (s3 != tmp) { x3 = solve(idx + 3, s3); } } if (x2) ans.insert(s2); if (x3) ans.insert(s3); return mp[{idx, tmp}] = (x2 || x3); } int main() { cin >> s; if (s.size() < 7) cout << 0; else { for (int i = 5; i < s.size() - 1; i++) solve(i, ""); cout << ans.size() << endl; for (auto i : ans) { cout << i << endl; } } }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int oo = 1000000009; const double eps = 1e-9; const int mod = 1000000007; bool x[10050], y[10050]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); string s; cin >> s; vector<string> ans; int i = s.size() - 2; for (; i >= 5; --i) { if (i == s.size() - 2) ans.push_back(s.substr(i, 2)), x[i] = 1; else if (i == s.size() - 3) ans.push_back(s.substr(i, 3)), y[i] = 1; else { if (y[i + 2] == 1 || (x[i + 2] == 1 && s.substr(i, 2) != s.substr(i + 2, 2))) ans.push_back(s.substr(i, 2)), x[i] = 1; if (x[i + 3] == 1 || (y[i + 3] == 1 && s.substr(i, 3) != s.substr(i + 3, 3))) ans.push_back(s.substr(i, 3)), y[i] = 1; } } sort(ans.begin(), ans.end()); ans.erase(unique(ans.begin(), ans.end()), ans.end()); cout << ans.size() << "\n"; for (int j = 0; j < ans.size(); ++j) { cout << ans[j] << "\n"; } }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import sys value = input() if (len(value) < 7): print(0) sys.exit(0) res = set() possible = {} possible[len(value)] = set([2]) if (len(value) > 7): possible[len(value)].add(3) possibleLen = [2, 3] for i in reversed(range(7, len(value) + 1)): possibleVal = possible.get(i, set()) for length in possibleVal: nextI = i - length val = value[nextI:i] res.add(val) for posLen in possibleLen: if (nextI >= 5 + posLen and value[nextI - posLen:nextI] != val): setNextI = possible.setdefault(nextI, set()) setNextI.add(posLen) print(len(res)) for val in sorted(res): print(val)
PYTHON3
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4 + 10, md = 1000000007; char str[maxn]; string s; set<string> res; int n, dp[maxn][6]; bool solve(int p, int l) { if (p + l - 1 == n - 1) { res.insert(s.substr(p, l)); return 1; } if (p + l - 1 > n - 1) return 0; int &ret = dp[p][l]; if (ret + 1) return ret; ret = 0; if (l == 2) { if (solve(p + l, 3)) { res.insert(s.substr(p, l)); ret |= 1; } if (s.substr(p, l) != s.substr(p + l, l) && solve(p + l, l)) { res.insert(s.substr(p, l)); ret |= 1; } } else if (l == 3) { if (solve(p + l, 2)) { res.insert(s.substr(p, l)); ret |= 1; } if (s.substr(p, l) != s.substr(p + l, l) && solve(p + l, l)) { res.insert(s.substr(p, l)); ret |= 1; } } return ret; } int main() { memset(dp, -1, sizeof dp); cin >> s; n = s.size(); for (int i = 5; i <= n - 1; ++i) { solve(i, 2); solve(i, 3); } cout << res.size() << endl; for (string s : res) cout << s << endl; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const ll inf = 1; const ll mod = 1E9; int vis[10010][4]; string a; int n; set<string> ms; int bctk(int pos, int len) { int &ret = vis[pos][len]; if (ret != -1) { return ret; } ret = 0; if (pos == n) { ret = 1; } else if (n - pos > 1) { string prev = ""; for (int i = len; i > 0; i--) { prev = prev + a[pos - i]; } string now = ""; for (int i = 0; i < 3 && pos + i < n; i++) { now = now + a[pos + i]; if (i > 0) { if (prev != now) { ret = max(ret, bctk(pos + i + 1, i + 1)); } } } } return ret; } int main() { ios::sync_with_stdio(false); cin >> a; n = (int)a.size(); memset(vis, -1, sizeof(vis)); for (int i = 5; i < n; i++) { bctk(i, 0); } for (int i = 0; i <= n; i++) { for (int j = 2; j < 4; j++) { if (vis[i][j] == 1) { string tmp = ""; for (int k = j; k > 0; k--) { tmp += a[i - k]; } ms.insert(tmp); } } } cout << (int)ms.size() << endl; for (auto it : ms) { cout << it << endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; template <typename T> inline void ckmax(T& x, T y) { x = (y > x ? y : x); } template <typename T> inline void ckmin(T& x, T y) { x = (y < x ? y : x); } namespace Fread { const int SIZE = 1 << 21; char buf[SIZE], *S, *T; inline char getchar() { if (S == T) { T = (S = buf) + fread(buf, 1, SIZE, stdin); if (S == T) return '\n'; } return *S++; } } // namespace Fread namespace Fwrite { const int SIZE = 1 << 21; char buf[SIZE], *S = buf, *T = buf + SIZE; inline void flush() { fwrite(buf, 1, S - buf, stdout); S = buf; } inline void putchar(char c) { *S++ = c; if (S == T) flush(); } struct NTR { ~NTR() { flush(); } } ztr; } // namespace Fwrite namespace Fastio { struct Reader { template <typename T> Reader& operator>>(T& x) { char c = Fread ::getchar(); T f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = Fread ::getchar(); } x = 0; while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = Fread ::getchar(); } x *= f; return *this; } Reader& operator>>(char& c) { c = Fread ::getchar(); while (c == '\n' || c == ' ') c = Fread ::getchar(); return *this; } Reader& operator>>(char* str) { int len = 0; char c = Fread ::getchar(); while (c == '\n' || c == ' ') c = Fread ::getchar(); while (c != '\n' && c != ' ') { str[len++] = c; c = Fread ::getchar(); } str[len] = '\0'; return *this; } Reader() {} } cin; const char endl = '\n'; struct Writer { template <typename T> Writer& operator<<(T x) { if (x == 0) { Fwrite ::putchar('0'); return *this; } if (x < 0) { Fwrite ::putchar('-'); x = -x; } static int sta[45]; int top = 0; while (x) { sta[++top] = x % 10; x /= 10; } while (top) { Fwrite ::putchar(sta[top] + '0'); --top; } return *this; } Writer& operator<<(char c) { Fwrite ::putchar(c); return *this; } Writer& operator<<(char* str) { int cur = 0; while (str[cur]) Fwrite ::putchar(str[cur++]); return *this; } Writer& operator<<(const char* str) { int cur = 0; while (str[cur]) Fwrite ::putchar(str[cur++]); return *this; } Writer() {} } cout; } // namespace Fastio const int MAXN = 1e4; int n; char s[MAXN + 5]; bool dp[MAXN + 5][2]; set<string> ans_set; int main() { Fastio ::cin >> (s + 1); n = strlen(s + 1); dp[n - 1][0] = 1; dp[n - 2][1] = 1; for (int i = n - 3; i >= 6; --i) { if (dp[i + 2][1] || (dp[i + 2][0] && (s[i] != s[i + 2] || s[i + 1] != s[i + 3]))) { dp[i][0] = 1; } if (dp[i + 3][0] || (dp[i + 3][1] && (s[i] != s[i + 3] || s[i + 1] != s[i + 4] || s[i + 2] != s[i + 5]))) { dp[i][1] = 1; } } for (int i = n - 1; i >= 6; --i) { if (dp[i][0]) { string str = ""; str += s[i]; str += s[i + 1]; ans_set.insert(str); } if (dp[i][1]) { string str = ""; str += s[i]; str += s[i + 1]; str += s[i + 2]; ans_set.insert(str); } } Fastio ::cout << ((int)(ans_set).size()) << Fastio ::endl; for (set<string>::iterator it = ans_set.begin(); it != ans_set.end(); ++it) { Fastio ::cout << (*it).c_str() << Fastio ::endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 10100; int n; string s; int f[maxn][5]; set<string> ret; void dfs(int st, int len, string ori) { if (st - len + 1 < 5 || f[st][len]) return; f[st][len] = 1; string tmp = ""; for (int i = st - len + 1; i <= st; i++) { tmp += s[i]; } if (tmp == ori) { f[st][len] = 0; return; } ret.emplace(tmp); dfs(st - len, 2, tmp); dfs(st - len, 3, tmp); } signed main() { cin >> s; n = s.length(); memset(f, 0, sizeof f); dfs(n - 1, 2, ""); dfs(n - 1, 3, ""); cout << ret.size() << endl; for (const auto x : ret) { cout << x << endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int CHAR_COUNT = 'z' - 'a' + 1; string s; vector<map<string, bool>> dp; bool F(int pos, const string& bad) { if (dp[pos].find(bad) != dp[pos].end()) return dp[pos][bad]; if (pos == (int)s.size()) return dp[pos][bad] = true; dp[pos][bad] = false; if (pos + 2 <= (int)s.size()) { if ((int)bad.size() != 2 || s.substr(pos, 2) != bad) { if (F(pos + 2, s.substr(pos, 2))) dp[pos][bad] = true; } } if (pos + 3 <= (int)s.size()) { if ((int)bad.size() != 3 || s.substr(pos, 3) != bad) { if (F(pos + 3, s.substr(pos, 3))) dp[pos][bad] = true; } } return dp[pos][bad]; } int main() { ios_base::sync_with_stdio(false); cin >> s; dp.resize(s.size() + 1); set<string> res; for (int i = 5; i < (int)s.size(); ++i) { if (i + 2 <= (int)s.size() && F(i + 2, s.substr(i, 2))) res.insert(s.substr(i, 2)); if (i + 3 <= (int)s.size() && F(i + 3, s.substr(i, 3))) res.insert(s.substr(i, 3)); } cout << res.size() << '\n'; for (const auto& s : res) cout << s << '\n'; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> template <typename Arg1> void __f(const char* name, Arg1&& arg1) { std::cout << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); std::cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } using namespace std; map<pair<long long, string>, long long> dp; set<string> st; string s; void f(long long i, string pr) { if (dp.count(make_pair(i, pr))) return; if (i - 2 > 3) { string g = s.substr(i - 1, 2); if (g != pr) { st.insert(g), f(i - 2, g); } } if (i - 3 > 3) { string g = s.substr(i - 2, 3); if (g != pr) { st.insert(g), f(i - 3, g); } } dp[make_pair(i, pr)] = 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long i, j, n, m, k, cnt = 0, ans = 0, t = 1; cin >> s; n = s.length(); f(n - 1, ""); cout << st.size() << '\n'; for (auto it : st) cout << it << '\n'; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.util.Scanner; import java.util.TreeSet; public class AlyonaAndStrings { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String word = sc.next(); TreeSet<String> set = new TreeSet<>(); boolean[] dp = new boolean[word.length()]; int num = findWords(set, dp, word); System.out.println(num); for(String x: set){ System.out.println(x); } } private static int findWords(TreeSet<String> set, boolean[] dp, String word) { if(word.length() <= 6) return 0; int len = word.length() - 1; dp[len] = false; dp[len - 1] = true; dp[len - 2] = true; set.add(word.substring(len-1, len+1)); if((len + 1 - 2) > 5) set.add(word.substring(len-2, len+1)); for(int i = len - 3; i >= 5; i--){ if(dp[i+2]){ if(!word.substring(i, i+2).equals(word.substring(i+2, i+4))){ dp[i] = true; set.add(word.substring(i, i+2)); } else if(!set.contains(word.substring(i, i+2))){ dp[i] = true; set.add(word.substring(i, i+2)); } if(i+5 < len && dp[i+5]){ dp[i] = true; set.add(word.substring(i, i+2)); } } if(dp[i+3]){ if(i+6 > len+1){ dp[i] = true; set.add(word.substring(i, i+3)); } else if(!word.substring(i, i+3).equals(word.substring(i+3, i+6))){ dp[i] = true; set.add(word.substring(i, i+3)); } else if(!set.contains(word.substring(i, i+2))){ dp[i] = true; set.add(word.substring(i, i+2)); } else if(i+5 < len && dp[i+5]){ dp[i] = true; set.add(word.substring(i, i+3)); } } } return set.size(); } // public static void main(String[] args) { // Scanner sc = new Scanner(System.in); // String word = sc.next(); // // int no = 1; // if(word.length() <= 5) // no = 0; // // // TreeSet<String> set = new TreeSet<>(); // int i = word.length()-1; // int j = word.length()-1; // int len = word.length()-1; // int chg = 6; // int chg2 = 6; // while(i >= chg && j >=chg2 && no != 0){ // // if(len - i != 1){ // if(i+3 <= len + 1 && i+1 <= len && word.substring(i-1, i+1).equals(word.substring(i+1, i+3))) // chg ++; // else // set.add(word.substring(i-1, i+1)); // } // if(j-2 >= 5 && len - j != 1){ // if(i+4 <= len + 1 && i+1 <= len && word.substring(i-2, i+1).equals(word.substring(i+1, i+4))) // chg2 ++; // else // set.add(word.substring(j-2, j+1)); // } // i += -1; // j += -1; // // } // int x = set.size(); // System.out.println(x); // for(String y: set){ // System.out.println(y); // } // // // } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 10010; const int A = 26; int d2[N], d3[N]; int main() { int i, j; string s; cin >> s; int n = s.length(); d2[n] = d3[n] = 1; for (i = n - 2; i >= 0; --i) { d2[i] |= d2[i + 2] & ((i + 2 > n - 2) || (s.substr(i, 2) != s.substr(i + 2, 2))); d2[i] |= d3[i + 2]; if (i <= n - 3) { d3[i] |= d3[i + 3] & ((i + 3 > n - 3) || (s.substr(i, 3) != s.substr(i + 3, 3))); d3[i] |= d2[i + 3]; } } std::vector<string> ans; for (j = 5; j < n - 1; ++j) { if (d2[j]) ans.push_back(s.substr(j, 2)); if (d3[j] & (j + 3 <= n)) ans.push_back(s.substr(j, 3)); } sort(ans.begin(), ans.end()); std::vector<string>::iterator it, iter; it = unique(ans.begin(), ans.end()); cout << distance(ans.begin(), it) << endl; for (iter = ans.begin(); iter != it; ++iter) cout << *iter << endl; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.io.*; import java.util.*; public class reberland_linguistics { static int [][] dp; static String s; static TreeSet<String> hs; static void rec(int start, int len) { if (start <= 4 || dp[start][len-2] == 1) return; dp[start][len-2] = 1; String a = s.substring(start, start+len); hs.add(a); if (len == 3 || !a.equals(s.substring(start-2, start))) rec(start-2, 2); if (len == 2 || !a.equals(s.substring(start-3, start))) rec(start-3, 3); } public static void main(String args[])throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); hs = new TreeSet<String>() ; s = br.readLine(); if (s.length() <= 6) { out.println(0); out.close(); out.flush(); return; } else { dp = new int [s.length()][2]; int n = s.length(); rec(n-2, 2); rec(n-3, 3); } out.println(hs.size()); StringBuilder ans = new StringBuilder(); for (String s: hs) ans = ans.append(s+"\n"); out.println(ans); out.close(); out.flush(); } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write def in_num(): return int(raw_input()) def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): pr(' '.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(int,stdin.read().split()) range = xrange # not for python 3.0+ r=raw_input().strip() n=len(r) if n<=6: pr_num(0) exit() dp=[[0,0] for i in range(n)] if n>=7: dp[n-2][0]=1 if n>=8: dp[n-3][1]=1 i=n-4 for i in range(n-4,4,-1): if dp[i+2][1] or (dp[i+2][0] and r[i:i+2]!=r[i+2:i+4]): dp[i][0]=1 if dp[i+3][0] or (dp[i+3][1] and r[i:i+3]!=r[i+3:i+6]): dp[i][1]=1 d=Counter() ans=0 for i in range(n): if dp[i][0]: r1=r[i:i+2] if not d[r1]: d[r1]=1 ans+=1 if dp[i][1]: r1=r[i:i+3] if not d[r1]: d[r1]=1 ans+=1 pr_num(ans) for i in sorted(d): pr(i+'\n')
PYTHON
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; template <typename Tp> inline void read(Tp &x) { static char c; static bool neg; x = 0, c = getchar(), neg = false; for (; !isdigit(c); c = getchar()) { if (c == '-') { neg = true; } } for (; isdigit(c); c = getchar()) { x = x * 10 + c - '0'; } if (neg) { x = -x; } } const int N = 1e4 + 5; string str, temp; int n; bool vis[N]; vector<string> vec; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> str; if (str.length() == 5UL) { puts("0"); return 0; } str = str.substr(5); n = (int)str.length(); vis[n] = true; for (int i = n - 1; i >= 0; --i) { for (int len = 2; len <= 3; ++len) { if (i + len <= n) { temp = str.substr(i, len); if (((int)str.find(temp, i + len) != i + len || vis[i + 5]) && vis[i + len]) { vec.emplace_back(temp); vis[i] = true; } } } } sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); cout << vec.size() << "\n"; for (const auto &s : vec) { cout << s << "\n"; } }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 1e4 + 10; int dp[N][5], n; set<string> ans; string s; int main() { cin >> s; n = s.size(); s = '.' + s; if (n <= 6) puts("0"); else { dp[n + 1][2] = dp[n + 1][3] = 1; for (int i = n - 1; i > 5; i--) { if (i <= n - 3) dp[i][2] = (s.substr(i, 2) == s.substr(i + 2, 2) ? 0 : dp[i + 2][2]) || dp[i + 2][3]; else dp[i][2] = dp[i + 2][2]; if (i <= n - 5) dp[i][3] = (s.substr(i, 3) == s.substr(i + 3, 3) ? 0 : dp[i + 3][3]) || dp[i + 3][2]; else dp[i][3] = dp[i + 3][2] || dp[i + 3][2]; if (dp[i][2]) ans.insert(s.substr(i, 2)); if (dp[i][3]) ans.insert(s.substr(i, 3)); } cout << ans.size() << endl; for (set<string>::iterator it = ans.begin(); it != ans.end(); it++) cout << *it << endl; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; string s; bool dp[10100]; void read() { cin >> s; } void solve() { if (s.length() < 7) { cout << 0 << endl; return; } set<string> ans; dp[s.length()] = true; for (int i = s.length() - 2; i >= 5; i--) { if (i == (s.length() - 2)) { dp[i] = true; ans.insert(s.substr(i, 2)); } else if (i == (s.length() - 3)) { dp[i] = true; ans.insert(s.substr(i, 3)); } else if (i == (s.length() - 4)) { if (s.substr(i, 2) != s.substr(i + 2, 2)) { dp[i] = true; ans.insert(s.substr(i, 2)); } } else { if ((dp[i + 2] && s.substr(i, 2) != s.substr(i + 2, 2)) || (dp[i + 2] && dp[i + 5])) { dp[i] = true; ans.insert(s.substr(i, 2)); } if ((dp[i + 3] && (i == (s.length() - 5) || s.substr(i, 3) != s.substr(i + 3, 3))) || (dp[i + 3] && dp[i + 5])) { dp[i] = true; ans.insert(s.substr(i, 3)); } } } cout << ans.size() << endl; for (set<string>::iterator it = ans.begin(); it != ans.end(); ++it) { cout << *it << endl; } } int main() { std::ios::sync_with_stdio(false); read(); solve(); return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 1e4; string s; int n, dp[N + 10][5]; bool dfs(int i, int l) { if (i == n) return dp[i][l] = true; int now = 0; if (dp[i][l] != -1) return dp[i][l]; if (l == 2) { if (i + 1 < n && (s[i] != s[i - 2] || s[i + 1] != s[i - 1])) now |= dfs(i + 2, 2); if (i + 2 < n) now |= dfs(i + 3, 3); } else { if (i + 1 < n) now |= dfs(i + 2, 2); if (i + 2 < n && (s[i] != s[i - 3] || s[i + 1] != s[i - 2] || s[i + 2] != s[i - 1])) now |= dfs(i + 3, 3); } return dp[i][l] = now; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; n = s.size(); memset(dp, -1, sizeof(dp)); dfs(7, 2); for (int i = 8; i <= n; i++) dfs(i, 2), dfs(i, 3); set<string> st; for (int i = 5; i <= n; i++) { if (dp[i + 2][2] == 1) st.insert(s.substr(i, 2)); if (dp[i + 3][3] == 1) st.insert(s.substr(i, 3)); } cout << st.size() << endl; for (auto x : st) cout << x << endl; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pdi = pair<double, int>; using pli = pair<ll, int>; using triple = tuple<int, int, int>; const int N = 3e5 + 7; const int mod = 1e9 + 7; string s; bool dp[N][5]; set<string> res; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); { int may = 0; int cai = 0; int bien = 1; int nay = 2; int la = 3; int de = 4; int khoi = 5; int bi = 6; int trung = 7; int code = 8; int oan = 9; } int tc = 1; while (tc--) { cin >> s; reverse(s.begin(), s.end()); dp[1][2] = dp[2][3] = true; for (uint32_t i = 3; i < s.size() - 5; ++i) { dp[i][2] = dp[i - 2][3] || (dp[i - 2][2] && s.substr(i - 1, 2) != s.substr(i - 3, 2)); dp[i][3] = dp[i - 3][2] || (i > 4 && dp[i - 3][3] && s.substr(i - 2, 3) != s.substr(i - 5, 3)); } reverse(s.begin(), s.end()); for (uint32_t i = 1; i < s.size() - 5; ++i) { if (dp[i][2]) res.insert(s.substr(s.size() - 1 - i, 2)); if (dp[i][3]) res.insert(s.substr(s.size() - 1 - i, 3)); } cout << res.size() << '\n'; for (string x : res) cout << x << '\n'; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long maxn = 1e5 + 7; const long long maxe = 1e6 + 7; const long long INF = 1e9 + 7; const double PI = acos(-1); int dx[4] = {0, 0, 1, -1}; int dy[4] = {-1, 1, 0, 0}; int len; string s; const int maxm = 11000; int dp[maxm][5]; set<string> ans; string gs(int i, int l) { return s.substr(i, l); } bool ok(int p, int l) { if (p > len) return false; if (p == len) return true; if (dp[p][l] != -1) return dp[p][l]; for (int i = 2; i < 4; i++) { if (ok(p + l, i) && gs(p, l) != gs(p + l, i)) { return dp[p][l] = true; } } return dp[p][l] = false; } int main() { cin >> s; len = s.length(); memset(dp, -1, sizeof(dp)); for (int i = 5; i < len; i++) { for (int j = 2; j < 4; j++) { if (ok(i, j)) ans.insert(gs(i, j)); } } cout << ans.size() << endl; set<string>::iterator it = ans.begin(); for (; it != ans.end(); it++) cout << *it << endl; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; string S; int dp[10005][4]; int main() { cin >> S; dp[S.size() - 2][2] = 1; dp[S.size() - 3][3] = 1; for (int i = S.size() - 1; i >= 0; i--) { if (dp[i + 3][2] == 1) dp[i][3] = 1; if (dp[i + 2][3] == 1) dp[i][2] = 1; if (dp[i + 3][3] == 1) { if (S.substr(i, 3) != S.substr(i + 3, 3)) dp[i][3] = 1; } if (dp[i + 2][2] == 1) { if (S.substr(i, 2) != S.substr(i + 2, 2)) dp[i][2] = 1; } } vector<string> V; for (int i = 5; i < S.size(); i++) { if (dp[i][2] == 1) V.push_back(S.substr(i, 2)); if (dp[i][3] == 1) V.push_back(S.substr(i, 3)); } sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end()); cout << V.size() << endl; for (int i = 0; i < V.size(); i++) cout << V[i] << endl; return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; /** Class: ReberlandLinguistics.java * @author Yury Park * @version 1.0 <p> * Course: * Written / Updated: Jun 14, 2016 * * This Class - See accompanying pdf file. */ public class ReberlandLinguistics { StringBuilder wordSB; public ReberlandLinguistics() { try { BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); //Read the first line this.wordSB = new StringBuilder(rd.readLine()); rd.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } //end public ReberlandLinguistics /* The "root" of the word must contain at least 5 letters. The "suffixes" are composed of a concatenation * of either 2 or 3-letter words. Rule: it is not allowed to append the same string twice in a row. * How many possible suffixes are there? */ private void computeNumSuffixes() { /* We may assume that the given word is at least 5 letters long. * Base case: if word is exactly 5 or 6 letters long, there is no possible suffix. */ if (wordSB.length() <= 6) { System.out.println("0"); return; } HashSet<String> validSuffixHS = new HashSet<>(); /* Use dynamic programming. Start from the end of the word and go backwards. * Let's initialize an array: * * isValid[i][2] -- this will keep track of whether the i-th indexed character thru the * last character of the given word can be validly broken down into one or more suffixes, where * the two characters at i and i+1 is one suffix. * * isValid[i][3] -- just like isValid[i][2], except the three characters at i, i+1 and i+2 is * a valid suffix. * * Remember -- it is not allowed to append the same suffix string twice in a row. Suffixes are considered * the same IFF they are the same length AND if they consist of the same sequence of characters. * * With the above rule in mind, the recursive substructure is: * isValid[i][2] = (isValid[i+2][3] || isValid[i+2][2] && wordSB.substring(i, i+2) != wordSB.substring(i+2, i+4)); * isValid[i][3] = (isValid[i+3][2] || isValid[i+3][3] && wordSB.substring(i, i+3) != wordSB.substring(i+3, i+6)); * * The base cases are as follows, assuming the given word is 7 characters or more: * isValid[wordLength - 2][2] = true //assuming word is at least 7 characters * isValid[wordLength - 2][3] = false //assuming word is at least 7 characters * isValid[wordLength - 3][2] = false //assuming word is at least 8 characters * isValid[wordLength - 3][3] = true //assuming word is at least 8 characters * * */ boolean[][] isValid = new boolean[wordSB.length()][4]; //initialized to false by default //base cases, as detailed in above comment isValid[wordSB.length() - 2][2] = true; validSuffixHS.add(wordSB.substring(wordSB.length() - 2)); isValid[wordSB.length() - 2][3] = false; if (wordSB.length() >= 8) { isValid[wordSB.length() - 3][2] = false; isValid[wordSB.length() - 3][3] = true; validSuffixHS.add(wordSB.substring(wordSB.length() - 3)); } //abracadab /* We'll do a for loop with i starting at the 4nd-to-last character until the index decrements * all the way to index 4 (which denotes the 5th character of the word) */ for (int i = wordSB.length() - 4; i > 4; --i) { /* Update: instead of the below 2 lines of code, we broke it up into if statements below, in order to also enable * adding valid suffixes to the ArrayList. */ // isValid[i][2] = (isValid[i+2][3] || isValid[i+2][2] && wordSB.substring(i, i+2) != wordSB.substring(i+2, i+4)); // isValid[i][3] = (isValid[i+3][2] || isValid[i+3][3] && wordSB.substring(i, i+3) != wordSB.substring(i+3, i+6)); if (isValid[i+2][3] || isValid[i+2][2] && !wordSB.substring(i, i+2).equals(wordSB.substring(i+2, i+4))) { isValid[i][2] = true; validSuffixHS.add(wordSB.substring(i, i+2)); } if (isValid[i+3][2] || isValid[i+3][3] && !wordSB.substring(i, i+3).equals(wordSB.substring(i+3, i+6))) { isValid[i][3] = true; validSuffixHS.add(wordSB.substring(i, i+3)); } } //end for i /* This will keep track of all the valid suffixes. We won't worry about duplicate entries here; * we'll take care of that later. */ ArrayList<String> validSuffixAL = new ArrayList<>(validSuffixHS); Collections.sort(validSuffixAL); System.out.println(validSuffixAL.size()); for (String suffix : validSuffixAL) { System.out.println(suffix); } } //end private void computeNumSuffixes() public static void main(String[] args) { ReberlandLinguistics rl = new ReberlandLinguistics(); rl.computeNumSuffixes(); } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; bool can[100007][4]; string s; string get2(int pos) { string ans; ans += s[pos]; ans += s[pos + 1]; return ans; } string get3(int pos) { string ans; ans += s[pos]; ans += s[pos + 1]; ans += s[pos + 2]; return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s; int n = (int)s.length(); can[n][2] = true; can[n][3] = true; for (int i = n - 1; i >= 4; i--) { string goSuff; for (int j = i; j <= i + 2; j++) { if (j >= n) break; goSuff += s[j]; if (goSuff.length() == 1) continue; if (can[i + goSuff.length()][goSuff.length()]) { can[i][0] = true; if (get2(i - 2) != goSuff) { can[i][2] = true; } if (get3(i - 3) != goSuff) { can[i][3] = true; } } } } set<string> ans; for (int i = 5; i < n; i++) { string goSuff; for (int j = i; j <= i + 2; j++) { if (j >= n) break; goSuff += s[j]; if (goSuff.length() == 1) continue; if (can[i + goSuff.length()][goSuff.length()]) { ans.insert(goSuff); } } } cout << ans.size() << '\n'; for (auto t : ans) { cout << t << '\n'; } }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.io.*; import java.util.Collections; import java.util.InputMismatchException; import java.util.LinkedList; public class Cf2904C { private static InputReader in = new InputReader(System.in); private static OutputWriter out = new OutputWriter(System.out); private static void solve() throws Exception { String s = in.readString(); char[] str = s.toCharArray(); boolean[][] isSuffix = new boolean[4][s.length()]; isSuffix[2][s.length()-2] = true; isSuffix[3][s.length()-3] = true; isSuffix[2][s.length()-5] = true; isSuffix[3][s.length()-5] = true; if((str[s.length()-2]!=str[s.length()-4])||(str[s.length()-1]!=str[s.length()-3])){ isSuffix[2][s.length()-4] = true; } for (int z = str.length-6; z >= 5; z--) { for(int len = 2; len<=3; ++len) { if(len==2){ if(isSuffix[3][z+2]){ isSuffix[2][z] = true; continue; } if(isSuffix[2][z+2]){ if((str[z]!=str[z+2])||(str[z+1]!=str[z+3])){ isSuffix[2][z] = true; } } } else{ if(isSuffix[2][z+3]){ isSuffix[3][z] = true; continue; } if(isSuffix[3][z+3]){ if((str[z]!=str[z+3])||(str[z+1]!=str[z+4])||(str[z+2]!=str[z+5])){ isSuffix[3][z] = true; } } } } } int count = 0; LinkedList<String> answers = new LinkedList<>(); for (int i = 97; i < 123; ++i) { for (int j = 97; j < 123; ++j) { for (int z = str.length - 2; z >= 5; z--) { if (z == str.length - 3) { continue; } if ((isSuffix[2][z]&&((str[z] == i) && (str[z + 1] == j)))) { count++; answers.add(s.substring(z, z + 2)); break; } } } } for (int i = 97; i < 123; ++i) { for (int j = 97; j < 123; ++j) { for (int k = 97; k < 123; ++k) { for (int z = str.length - 3; z >= 5; z--) { if (z == str.length - 4) { continue; } if((isSuffix[3][z])&& ((str[z] == i) && (str[z + 1] == j) && (str[z + 2] == k))) { count++; answers.add(s.substring(z, z + 3)); break; } } } } } Collections.sort(answers); out.println(count); for (String answer : answers) { out.println(answer); } } public static void main(String[] args) throws Exception { solve(); out.close(); } private static class InputReader { private InputStream stream; private byte[] buffer; private int currentIndex; private int bytesRead; public InputReader(InputStream stream) { this.stream = stream; buffer = new byte[131072]; } public InputReader(InputStream stream, int bufferSize) { this.stream = stream; buffer = new byte[bufferSize]; } private int read() throws IOException { if (currentIndex >= bytesRead) { currentIndex = 0; bytesRead = stream.read(buffer); if (bytesRead <= 0) { return -1; } } return buffer[currentIndex++]; } public String readString() throws IOException { int c = read(); while (!isPrintable(c)) { c = read(); } StringBuilder result = new StringBuilder(); do { result.appendCodePoint(c); c = read(); } while (isPrintable(c)); return result.toString(); } public int readInt() throws Exception { int c = read(); int sign = 1; while (!isPrintable(c)) { c = read(); } if (c == '-') { sign = -1; c = read(); } int result = 0; do { if ((c < '0') || (c > '9')) { throw new InputMismatchException(); } result *= 10; result += (c - '0'); c = read(); } while (isPrintable(c)); return sign * result; } public long readLong() throws Exception { int c = read(); int sign = 1; while (!isPrintable(c)) { c = read(); } if (c == '-') { sign = -1; c = read(); } long result = 0; do { if ((c < '0') || (c > '9')) { throw new InputMismatchException(); } result *= 10; result += (c - '0'); c = read(); } while (isPrintable(c)); return sign * result; } public double readDouble() throws Exception { int c = read(); int sign = 1; while (!isPrintable(c)) { c = read(); } if (c == '-') { sign = -1; c = read(); } boolean fraction = false; double multiplier = 1; double result = 0; do { if ((c == 'e') || (c == 'E')) { return sign * result * Math.pow(10, readInt()); } if ((c < '0') || (c > '9')) { if ((c == '.') && (!fraction)) { fraction = true; c = read(); continue; } throw new InputMismatchException(); } if (fraction) { multiplier /= 10; result += (c - '0') * multiplier; c = read(); } else { result *= 10; result += (c - '0'); c = read(); } } while (isPrintable(c)); return sign * result; } private boolean isPrintable(int c) { return ((c > 32) && (c < 127)); } } private static class OutputWriter { private PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
word = input() length = len(word) acceptable2 = [None] * length acceptable2[0] = True; acceptable2[1] = False; acceptable2[2] = True; acceptable2[3] = False acceptable3 = [None] * length acceptable3[0] = True; acceptable3[1] = False; acceptable3[2] = False; acceptable3[3] = True all_possible_suffixes = set() def is_acceptable(suffix, rest): if len(suffix) == 2: if acceptable3[len(rest)]: return True if acceptable2[len(rest)] and not rest.startswith(suffix): return True return False if len(suffix) == 3: if acceptable2[len(rest)]: return True if acceptable3[len(rest)] and not rest.startswith(suffix): return True return False for i in range(length - 1, 4, -1): root = word[:i] suffixes = word[i:] if len(suffixes) < 2: continue first = suffixes[:2] rest = suffixes[2:] if is_acceptable(first, rest): all_possible_suffixes.add(first) acceptable2[len(suffixes)] = True else: acceptable2[len(suffixes)] = False if len(suffixes) < 3: continue first = suffixes[:3] rest = suffixes[3:] if is_acceptable(first, rest): all_possible_suffixes.add(first) acceptable3[len(suffixes)] = True else: acceptable3[len(suffixes)] = False print(len(all_possible_suffixes)) for s in sorted(list(all_possible_suffixes)): print(s)
PYTHON3
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5; set<string> S; set<string>::iterator it; bool vis[maxn][10]; char s[maxn]; int n, i; void dfs(int o, int m) { if (vis[o][m] || o - m < 5) return; vis[o][m] = 1; string l(s + o - m + 1, s + o + 1), r(s + o - m * 2 + 1, s + o + 1 - m); S.insert(l); if (l != r) dfs(o - m, m); int x = 5 - m; dfs(o - m, x); } int main() { scanf("%s", s + 1); n = strlen(s + 1); dfs(n, 2); dfs(n, 3); printf("%d\n", S.size()); it = S.begin(); while (it != S.end()) { cout << *it << endl; it++; } return 0; }
CPP
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
import java.io.*; import java.lang.*; import java.util.*; // Sachin_2961 submission // public class CodeforcesA { static TreeSet<String>treeSet = new TreeSet<>(); public void solve() { char[]a = fs.next().toCharArray(); int n = a.length; boolean[][] dp = new boolean[n+1][2]; dp[n][0] = dp[n][1] = true; dp[n-2][0] = true; dp[n-3][1] = true; for (int i = n-4; i >= 5; i--) { dp[i][0] = dp[i+2][1] || (i+3 < n && dp[i+2][0] && !(a[i]==a[i+2] && a[i+1] == a[i+3])); dp[i][1] = dp[i+3][0] || (i+5 < n && dp[i+3][1] && !(a[i]==a[i+3] && a[i+1] == a[i+4] && a[i+2] == a[i+5])); } for (int i = 5; i < n; i++) { if (dp[i][0]) treeSet.add("" + a[i] + a[i+1]); if (dp[i][1]) treeSet.add("" + a[i] + a[i+1] + a[i+2]); } System.out.println(treeSet.size()); for (String s : treeSet) System.out.println(s); } static boolean multipleTestCase = false; static FastScanner fs; static PrintWriter out; public void run(){ fs = new FastScanner(); out = new PrintWriter(System.out); int tc = (multipleTestCase)?fs.nextInt():1; while (tc-->0)solve(); out.flush(); out.close(); } public static void main(String[]args){ try{ new CodeforcesA().run(); }catch (Exception e){ e.printStackTrace(); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
JAVA
666_A. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: <image>, where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}. Input The only line contains a string s (5 ≀ |s| ≀ 104) consisting of lowercase English letters. Output On the first line print integer k β€” a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. Examples Input abacabaca Output 3 aca ba ca Input abaca Output 0 Note The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
2
7
#include <bits/stdc++.h> using namespace std; char s[10005]; map<int, int> flag[10005]; set<string> ans; int code(const string &s) { int ret = 0; for (int i = 0; i < s.size(); i++) ret = ret * 27 + (s[i] - 'a' + 1); return ret; } void go(int n, int last) { if (flag[n].count(last)) return; flag[n][last] = 1; if (n >= 7) { string suf = ""; suf += s[n - 2]; suf += s[n - 1]; int c = code(suf); if (c != last) { ans.insert(suf); go(n - 2, c); } } if (n >= 8) { string suf = ""; suf += s[n - 3]; suf += s[n - 2]; suf += s[n - 1]; int c = code(suf); if (c != last) { ans.insert(suf); go(n - 3, c); } } } int main() { scanf("%s", s); go(strlen(s), 0); printf("%d\n", (int)ans.size()); for (set<string>::iterator it = ans.begin(); it != ans.end(); it++) printf("%s\n", it->c_str()); }
CPP