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> using namespace std; const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } string s; set<string> mp; int seen[10009][6]; void solve(int i, int pr) { if (i <= 5) return; if (seen[i][pr]) return; seen[i][pr] = 1; mp.insert(s.substr(i - 1, 2)); if (pr != 2 || s.substr(i - 1, 2) != s.substr(i + 1, 2)) solve(i - 2, 2); if (i > 6) { mp.insert(s.substr(i - 2, 3)); if (pr != 3 || s.substr(i - 2, 3) != s.substr(i + 1, 3)) solve(i - 3, 3); } } char ss[112345]; int main() { int i, j, n; scanf("%s", ss); s = ss; solve(s.size() - 1, 0); printf("%d\n", (int)mp.size()); for (const string &s : mp) printf("%s\n", s.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
#include <bits/stdc++.h> using namespace std; string s; map<string, bool> Hash; map<pair<int, int>, bool> HASH; int spl[1000011], spr[1000011]; bool ck(int l, int r) { if (HASH[make_pair(l, r)] == true) return false; return true; } int main() { string a, b; cin >> s; int n = s.size(); int q, p, l, r; q = 0; p = 0; if (n > 6) { a = ""; a += s[n - 2]; a += s[n - 1]; Hash[a] = true; spl[++p] = n - 2; spr[p] = n - 1; } if (n > 7) { a = ""; a += s[n - 3]; a += s[n - 2]; a += s[n - 1]; Hash[a] = true; spl[++p] = n - 3; spr[p] = n - 1; } while (q < p) { b = ""; q++; for (int i = spl[q]; i <= spr[q]; i++) b += s[i]; r = spl[q] - 1; l = r - 1; if (l > 4 && ck(l, r)) { a = ""; for (int i = l; i <= r; i++) a += s[i]; if (a != b) { Hash[a] = true; HASH[make_pair(l, r)] = true; ++p; spl[p] = l; spr[p] = r; } } l = r - 2; if (l > 4 && ck(l, r)) { a = ""; for (int i = l; i <= r; i++) a += s[i]; if (a != b) { Hash[a] = true; HASH[make_pair(l, r)] = true; ++p; spl[p] = l; spr[p] = r; } } } cout << Hash.size() << endl; for (map<string, bool>::iterator it = Hash.begin(); it != Hash.end(); it++) { cout << it->first << 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> #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx,avx2,sse,sse2") using namespace std; using ll = long long; template <typename F> void multitest(F func) { int t; cin >> t; while (t--) func(); } void report(int ok) { cout << (ok ? "YES" : "NO") << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); string s; cin >> s; s = s.substr(5, s.size()); set<string> suf; vector<array<int, 2>> can(s.size() + 1); int n = s.size(); can[s.size()] = {1, 1}; for (int end = (int)s.size() - 1; end >= 0; end--) { can[end] = {0, 0}; if (end + 1 < n) { string t = s.substr(end, 2); int oof = 0; if (end + 3 < n) oof = t != s.substr(end + 2, 2) && can[end + 2][0]; can[end][0] = can[end + 2][1] | oof; } if (end + 2 < s.size()) { string t = s.substr(end, 3); int oof = 0; if (end + 5 < n) oof = t != s.substr(end + 3, 3) && can[end + 3][1]; can[end][1] = can[end + 3][0] | oof; } if (can[end][0]) suf.insert(s.substr(end, 2)); if (can[end][1]) suf.insert(s.substr(end, 3)); } cout << suf.size() << '\n'; for (auto i : suf) cout << 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; void itval(istream_iterator<string> it) {} template <typename T, typename... Args> void itval(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; itval(++it, args...); } const long long int MOD = 1e9 + 7; template <typename T> inline void print(T x) { cout << x << "\n"; } template <typename T> inline void printvec(T x) { for (auto a : x) cout << a << ' '; cout << '\n'; } struct custom { bool operator()(const pair<long long int, long long int> &p1, const pair<long long int, long long int> &p2) const { if (p1.first == p2.first) return p2.second < p1.second; return p1.first < p2.first; } }; long long int get_pow(long long int a, long long int b) { long long int res = 1; while (b) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; b >>= 1; } return res; } const long long int N = 1e5 + 10, inf = 4e18; char s[N]; bool dp[N][4]; void solve() { scanf("%s", s); int n = 0; for (int i = 0; 1; i++) { if (!s[i]) { n = i; break; } } dp[n][2] = 1; dp[n][3] = 1; set<string> ans; for (int i = n - 2; i >= 5; i--) { for (int j = 2; j <= 3; j++) { string a = "", b = ""; for (int k = i; k < i + j; k++) { a.push_back(s[k]); b.push_back(s[k + j]); } if (dp[i + j][j ^ 1] || (dp[i + j][j] && a != b)) { dp[i][j] = 1; ans.insert(a); } } } print(ans.size()); for (auto p : ans) cout << p << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int test = 1; clock_t z = clock(); for (long long int tes = (long long int)0; tes < (long long int)(test); tes++) { solve(); } fprintf(stderr, "Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC), fflush(stderr); 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:336777216") #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using ui = unsigned int; template <typename T> using less_priority_queue = priority_queue<T, vector<T>, greater<T>>; ll const mod = 1e9 + 7; auto mt = mt19937(chrono::steady_clock::now().time_since_epoch().count()); bool dp2[10005] = {}; bool dp3[10005] = {}; int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); string s; cin >> s; int n = s.size(); dp2[n] = 1; dp3[n] = 1; set<string> ans; for (int i = n - 2; i >= 5; i--) { if (dp3[i + 2] || (dp2[i + 2] && s.substr(i + 2, 2) != s.substr(i, 2))) { dp2[i] = 1; ans.insert(s.substr(i, 2)); } if (dp2[i + 3] || (dp3[i + 3] && s.substr(i + 3, 3) != s.substr(i, 3))) { dp3[i] = 1; ans.insert(s.substr(i, 3)); } } 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> using namespace std; const int N = 11111; const long long INF = 1e9 + 19; string s; void read() { cin >> s; } int dp[N][2]; void solve() { reverse(s.begin(), s.end()); for (int i = 0; i < 5; i++) s.pop_back(); int n = s.size(); dp[0][0] = 1; dp[2][0] = 1; dp[3][1] = 1; for (int i = 1; i < n; i++) { for (int t = 0; t < 2; t++) { int len = t + 2; if (dp[i][t ^ 1] == 1 || (dp[i][t] && s.substr(i - len, len) != s.substr(i, len))) dp[i + len][t] = 1; } } set<string> q; for (int i = 0; i < n; i++) if (dp[i][0] || dp[i][1]) { for (int len = 2; len <= 3; len++) { if (i + len <= n) { string t = s.substr(i, len); reverse(t.begin(), t.end()); q.insert(t); } } } printf("%d\n", (int)q.size()); for (auto s : q) cout << s << "\n"; } void stress() {} int main() { if (1) { int k = 1; for (int tt = 0; tt < k; tt++) { read(); solve(); } } else { stress(); } 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 = 20000 + 10; char str[maxn]; int n; bool oksuf[maxn][4]; set<string> ans; string getstr(int l, int r) { string s = ""; for (int i = l; i <= r; i++) { s.push_back(str[i]); } return s; } void prep() { memset(oksuf, 0, sizeof oksuf); n = strlen(str); oksuf[n][2] = true; oksuf[n][3] = true; oksuf[n - 2][2] = true; oksuf[n - 3][3] = true; for (int i = n - 1; i >= 0; i--) for (int j = 2; j <= 3; j++) { if (oksuf[i][j]) continue; for (int z = 2; z <= 3; z++) { if (i + j + z - 1 >= n) continue; if (j == z && getstr(i, i + j - 1) == getstr(i + j, i + j + z - 1)) continue; oksuf[i][j] |= oksuf[i + j][z]; } } } int main() { scanf("%s", str); n = strlen(str); prep(); ans.clear(); for (int i = 5; i < n; i++) { if (i + 2 <= n && oksuf[i][2]) { ans.insert(getstr(i, i + 1)); } if (i + 3 <= n && oksuf[i][3]) { ans.insert(getstr(i, i + 2)); } } printf("%d\n", (int)ans.size()); for (auto& s : ans) { cout << s << 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[10005][4]; map<string, bool> used; vector<string> v; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s; reverse(s.begin(), s.end()); dp[2][2] = true; dp[3][3] = true; int n = s.size(); for (int i = 2; i <= n - 5; ++i) { if (i >= 5 && dp[i - 2][3]) dp[i][2] = true; else if (i >= 4 && (s[i - 1] != s[i - 3] || s[i - 2] != s[i - 4]) && dp[i - 2][2]) dp[i][2] = true; if (i >= 5 && dp[i - 3][2]) dp[i][3] = true; else if (i >= 6 && (s[i - 1] != s[i - 4] || s[i - 2] != s[i - 5] || s[i - 3] != s[i - 6]) && dp[i - 3][3]) dp[i][3] = true; if (dp[i][2]) { string sk; sk = s[i - 2]; sk += s[i - 1]; if (used[sk] == false) { v.push_back(sk); used[sk] = true; } } if (dp[i][3]) { string sk; sk = s[i - 3]; sk += s[i - 2]; sk += s[i - 1]; if (used[sk] == false) { used[sk] = true; v.push_back(sk); } } } cout << v.size() << '\n'; for (int i = 0; i < v.size(); ++i) { reverse(v[i].begin(), v[i].end()); } sort(v.begin(), v.end()); for (int i = 0; i < v.size(); ++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
const memo = {}; const res = {}; function resolve(s, prev) { if (!memo[s + ' ' + prev]) { memo[s + ' ' + prev] = true; var suffix2 = s.slice(s.length - 2); if (suffix2 !== prev && s.length > 6) { res[suffix2] = true; resolve(s.slice(0, s.length - 2), suffix2); } var suffix3 = s.slice(s.length - 3); if (suffix3 !== prev && s.length > 7) { res[suffix3] = true; resolve(s.slice(0, s.length - 3), suffix3); } } } /* resolve('abcdeabzzzzzzzz', null); console.log(res); */ var s = readline(); resolve(s, null); var suffixes = Object.keys(res).sort(); print(suffixes.length); for (var i = 0; i < suffixes.length; i++) { print(suffixes[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
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)))
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> const int N = 10010; bool dp[2][N]; char s[N]; std::set<std::string> set; int main() { scanf("%s", s); int len = strlen(s); std::reverse(s, s + len); dp[0][1] = dp[1][2] = true; for (int i = 3; i < len - 5; ++i) { dp[0][i] = dp[1][i - 2] || dp[0][i - 2] && (s[i] != s[i - 2] || s[i - 1] != s[i - 3]); dp[1][i] = dp[0][i - 3] || dp[1][i - 3] && (s[i] != s[i - 3] || s[i - 1] != s[i - 4] || s[i - 2] != s[i - 5]); } for (int i = 1; i < len - 5; ++i) { if (dp[0][i]) { std::string str; str += s[i]; str += s[i - 1]; set.insert(str); } if (dp[1][i]) { std::string str; str += s[i]; str += s[i - 1]; str += s[i - 2]; set.insert(str); } } printf("%d\n", (int)set.size()); for (auto u : set) { std::cout << u << 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
import sys 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)) print(*suffixes_alph, sep='\n')
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; bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "Possible" : "Impossible"); exit(0); } void addmod(int &x, int y, int mod = 1000000007) { x += y; if (x >= mod) x -= mod; } void et() { puts("-1"); exit(0); } set<string> ss; string s; bool vis[10055][5]; string getIj(int i, int j) { string tmp = ""; for (int k = i; k <= j; k++) tmp += s[k]; return tmp; } void dfs(int i, int len) { if (vis[i][len]) return; if (i <= 4) return; vis[i][len] = 1; string pre = ""; if (len != 0) pre = getIj(i + 1, i + len); if (i - 1 > 4) { string two = getIj(i - 1, i); if (two != pre) { ss.insert(two); dfs(i - 2, 2); } } if (i - 2 > 4) { string three = getIj(i - 2, i); if (three != pre) { ss.insert(three); dfs(i - 3, 3); } } } void fmain() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s; dfs(s.size() - 1, 0); cout << ss.size() << endl; for (auto p : ss) cout << p << endl; } int main() { fmain(); 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> int main() { std::string s; std::cin >> s; int n = s.length(); std::set<std::string> res; bool dp2[n], dp3[n]; dp2[n - 1] = false; dp3[n - 1] = false; dp2[n - 2] = true; dp3[n - 2] = false; dp2[n - 3] = false; dp3[n - 3] = true; for (int i = n - 4; i >= 5; i--) { if (dp3[i + 2]) dp2[i] = true; else if (dp2[i + 2]) { std::string s1 = s.substr(i, 2); if (s1.compare(s.substr(i + 2, 2)) != 0) dp2[i] = true; else dp2[i] = false; } else dp2[i] = false; if (dp2[i + 3]) dp3[i] = true; else if (dp3[i + 3]) { std::string s1 = s.substr(i, 3); if (s1.compare(s.substr(i + 3, 3)) != 0) dp3[i] = true; else dp3[i] = false; } else dp3[i] = false; } for (int i = n - 1; i >= 5; i--) { if (dp2[i]) res.insert(s.substr(i, 2)); if (dp3[i]) res.insert(s.substr(i, 3)); } printf("%i\n", res.size()); for (auto i = res.begin(); i != res.end(); i++) std::cout << (*i) << 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; const int maxn = 1e4 + 5; string s; set<string> res; int tot = 0; int vis[4][maxn]; void dfs(int cnt, int endd, int pa) { if (endd - cnt < 4 || vis[cnt][endd]) return; string t; bool flg = 1; for (int i = 0; i < cnt; i++) { t += s[endd - cnt + i + 1]; if (pa != cnt || (pa == cnt && s[endd - cnt + i + 1] != s[endd + i + 1])) flg = 0; } if (!flg) { vis[cnt][endd] = 1; res.insert(t); dfs(2, endd - cnt, cnt); dfs(3, endd - cnt, cnt); } } int main(void) { ios::sync_with_stdio(false); cin >> s; int len = s.length(); dfs(2, len - 1, 0); dfs(3, len - 1, 0); cout << res.size() << endl; set<string>::iterator i; for (i = res.begin(); i != res.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
// package RatingBased.R1800; import java.io.*; import java.util.*; /* Author - Ribhav MADE ON - 1/2/2019 */ public class CF666A { static FastReader s; static PrintWriter out; static String INPUT = "abcdeabzzzzzzzz\n"; static TreeSet<String> set; static int[] bool; public static void main(String[] args) { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; out = new PrintWriter(System.out); s = new FastReader(oj); String str = s.next(); if(str.length() < 7){ System.out.println(0); return; } set = new TreeSet<>(); bool = new int[str.length()]; func(new StringBuilder(str.substring(5)).reverse().toString(), null, 0); out.println(set.size()); // out.println(set); Iterator<String> iter = set.iterator(); StringBuilder ans = new StringBuilder(); while(iter.hasNext()){ String s1 = iter.next(); ans.append(s1); ans.append("\n"); } out.println(ans.toString()); if (!oj) { System.out.println(Arrays.deepToString(new Object[]{System.currentTimeMillis() - time + " ms"})); } out.flush(); } private static void func(String str, String prev, int start) { if(prev == null){ if(str.length() == 2) { set.add(new StringBuilder(str).reverse().toString()); } else { String str1 = new StringBuilder(str.substring(0,2)).reverse().toString(); String str2 = new StringBuilder(str.substring(0,3)).reverse().toString(); func(str.substring(2), str1, start + 2); set.add(str1); bool[start] = 1; func(str.substring(3), str2, start + 3); set.add(str2); bool[start] = 2; } return; } if(str.length() == 1 || str.length() == 0){ return; } if(bool[start] == 2){ return; } int len = prev.length(); String str1 = new StringBuilder(str.substring(0,2)).reverse().toString(); if(len == 2){ if(str1.equals(prev)) { if(str.length() >= 3) { String str2 = new StringBuilder(str.substring(0,3)).reverse().toString(); func(str.substring(3), str2, start + 3); set.add(str2); } bool[start] = 1; return; }else { func(str.substring(2), str1, start + 2); set.add(str1); bool[start] = 1; if(str.length() >= 3) { String str2 = new StringBuilder(str.substring(0,3)).reverse().toString(); func(str.substring(3), str2, start + 3); set.add(str2); } bool[start] = 2; } } else { if(str.length() >= 3) { String str2 = new StringBuilder(str.substring(0,3)).reverse().toString(); if(str2.equals(prev)) { func(str.substring(2), str1,start + 2); set.add(str1); bool[start] = 1; return; } else { func(str.substring(2), str1, start + 2); set.add(str1); bool[start] = 1; func(str.substring(3), str2,start + 3); set.add(str2); bool[start] = 2; } } else { func(str.substring(2), str1,start + 2); set.add(str1); bool[start] = 2; } } } static ArrayList<Long> printDivisors(long n) { // Note that this loop runs till square root ArrayList<Long> list = new ArrayList<>(); for (long i = 1; i <= Math.sqrt(n); i++) { if (n % i == 0) { if (n / i == i) { list.add(i); } else { list.add(i); list.add(n / i); } } } return list; } // GCD - Using Euclid theorem. private static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } // Extended euclidean algorithm // Used to solve equations of the form ax + by = gcd(a,b) // return array [d, a, b] such that d = gcd(p, q), ap + bq = d static long[] extendedEuclidean(long p, long q) { if (q == 0) return new long[]{p, 1, 0}; long[] vals = extendedEuclidean(q, p % q); long d = vals[0]; long a = vals[2]; long b = vals[1] - (p / q) * vals[2]; return new long[]{d, a, b}; } // X ^ y mod p static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } // Returns modulo inverse of a // with respect to m using extended // Euclid Algorithm. Refer below post for details: // https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/ static long inv(long a, long m) { long m0 = m, t, q; long x0 = 0, x1 = 1; if (m == 1) return 0; // Apply extended Euclid Algorithm while (a > 1) { q = a / m; t = m; m = a % m; a = t; t = x0; x0 = x1 - q * x0; x1 = t; } // Make x1 positive if (x1 < 0) x1 += m0; return x1; } // k is size of num[] and rem[]. // Returns the smallest number // x such that: // x % num[0] = rem[0], // x % num[1] = rem[1], // .................. // x % num[k-2] = rem[k-1] // Assumption: Numbers in num[] are pairwise // coprime (gcd for every pair is 1) static long findMinX(long num[], long rem[], long k) { int prod = 1; for (int i = 0; i < k; i++) prod *= num[i]; int result = 0; for (int i = 0; i < k; i++) { long pp = prod / num[i]; result += rem[i] * inv(pp, num[i]) * pp; } return result % prod; } // Binary search private static int binarySearch(int[] arr, int ele) { int low = 0; int high = arr.length - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == ele) { return mid; } else if (ele < arr[mid]) { high = mid - 1; } else { low = mid + 1; } } return -1; } // First occurence using binary search private static int binarySearchFirstOccurence(int[] arr, int ele) { int low = 0; int high = arr.length - 1; int ans = -1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == ele) { ans = mid; high = mid - 1; } else if (ele < arr[mid]) { high = mid - 1; } else { low = mid + 1; } } return ans; } // Last occurenece using binary search private static int binarySearchLastOccurence(int[] arr, int ele) { int low = 0; int high = arr.length - 1; int ans = -1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == ele) { ans = mid; low = mid + 1; } else if (ele < arr[mid]) { high = mid - 1; } else { low = mid + 1; } } return ans; } // Merge sort static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int[n1]; int R[] = new int[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(int arr[], int l, int r) { if (l < r) { int m = (l + r) / 2; sort(arr, l, m); sort(arr, m + 1, r); merge(arr, l, m, r); } } static void sort(int[] arr) { sort(arr, 0, arr.length - 1); } static class FastReader { InputStream is; public FastReader(boolean oj) { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; public int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } public boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public double nextDouble() { return Double.parseDouble(next()); } public char nextChar() { return (char) skip(); } public String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } public long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } public int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } return arr; } public int[] uniq(int[] arr) { Arrays.sort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } } } /* */
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 = 4004; const int mod = 1e9 + 7; string s; int n; map<string, int> was, used; int w[100001]; int dp[10010][10]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> s; n = s.length(); dp[n - 2][2] = 1; dp[n - 3][3] = 1; for (int i = n - 3; i >= 5; --i) { string t = s.substr(i, 2); if (dp[i + 2][3]) dp[i][2] = 1; if (dp[i + 2][2]) { string tb2 = s.substr(i + 2, 2); if (tb2 != t) dp[i][2] = 1; } t = s.substr(i, 3); if (dp[i + 3][2]) dp[i][3] = 1; if (dp[i + 3][3]) { string tb2 = s.substr(i + 3, 3); if (tb2 != t) dp[i][3] = 1; } } for (int i = 5; i < n; ++i) { if (i + 1 < n) { string t = s.substr(i, 2); if (dp[i][2]) was[t] = 1; } if (i + 2 < n) { string t = s.substr(i, 3); if (dp[i][3]) was[t] = 1; } } cout << was.size() << "\n"; for (auto it = was.begin(); it != was.end(); ++it) cout << it->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
// package codeforces.cf3xx.cf349.div1; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; /** * Created by hama_du on 2016/08/06. */ public class A { public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); char[] c = in.nextToken().toCharArray(); int n = c.length; boolean[][] word2 = new boolean[26][26]; boolean[][][] word3 = new boolean[26][26][26]; int[][] dp = new int[n+1][2]; dp[n][0] = 1; for (int i = n ; i >= 5 ; i--) { for (int l = 0 ; l <= 1 ; l++) { if (dp[i][l] == 0) { continue; } if (i+l+2 <= n) { char p0 = c[i], p1 = c[i+1], p2 = (l == 1) ? c[i+2] : 0; if (l == 0) { word2[p0-'a'][p1-'a'] = true; } else { word3[p0-'a'][p1-'a'][p2-'a'] = true; } if (l == 0 && c[i-2] == p0 && c[i-1] == p1) { } else { dp[i-2][0] = 1; } if (l == 1 && c[i-3] == p0 && c[i-2] == p1 && c[i-1] == p2) { } else { dp[i-3][1] = 1; } } else { dp[i-2][0] = 1; dp[i-3][1] = 1; } } } List<String> ans = new ArrayList<>(); String[] a = new String[26]; for (int i = 0; i < 26; i++) { a[i] = ""+(char)('a'+i); } for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if (word2[i][j]) { ans.add(a[i]+a[j]); } for (int k = 0; k < 26; k++) { if (word3[i][j][k]) { ans.add(a[i]+a[j]+a[k]); } } } } out.println(ans.size()); for (String ll : ans) { out.println(ll); } out.flush(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int[] nextInts(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } private int[][] nextIntTable(int n, int m) { int[][] ret = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ret[i][j] = nextInt(); } } return ret; } private long[] nextLongs(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) { ret[i] = nextLong(); } return ret; } private long[][] nextLongTable(int n, int m) { long[][] ret = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ret[i][j] = nextLong(); } } return ret; } private double[] nextDoubles(int n) { double[] ret = new double[n]; for (int i = 0; i < n; i++) { ret[i] = nextDouble(); } return ret; } private int next() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public char nextChar() { int c = next(); while (isSpaceChar(c)) c = next(); if ('a' <= c && c <= 'z') { return (char) c; } if ('A' <= c && c <= 'Z') { return (char) c; } throw new InputMismatchException(); } public String nextToken() { int c = next(); while (isSpaceChar(c)) c = next(); StringBuilder res = new StringBuilder(); do { res.append((char) c); c = next(); } while (!isSpaceChar(c)); return res.toString(); } public int nextInt() { int c = next(); while (isSpaceChar(c)) c = next(); int sgn = 1; if (c == '-') { sgn = -1; c = next(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c-'0'; c = next(); } while (!isSpaceChar(c)); return res*sgn; } public long nextLong() { int c = next(); while (isSpaceChar(c)) c = next(); long sgn = 1; if (c == '-') { sgn = -1; c = next(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c-'0'; c = next(); } while (!isSpaceChar(c)); return res*sgn; } public double nextDouble() { return Double.valueOf(nextToken()); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } }
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 = (int)1e5 + 5; string s; int n, can[maxn][2]; set<string> ans; int main() { cin >> s; n = s.size(); for (int i = n - 1; i >= 5; i--) { if (i == n - 1) { can[i][0] = 0; can[i][1] = 0; continue; } if (i == n - 2) { can[i][0] = 1; can[i][1] = 0; continue; } if (i == n - 3) { can[i][0] = 0; can[i][1] = 1; continue; } if (i == n - 4) { if (s[i] != s[i + 2] || s[i + 1] != s[i + 3]) can[i][0] = 1; can[i][1] = 0; continue; } if ((s[i] != s[i + 2] || s[i + 1] != s[i + 3]) && can[i + 2][0] == 1) can[i][0] = 1; if (can[i + 2][1] == 1) can[i][0] = 1; if (can[i + 3][0] == 1) can[i][1] = 1; if (can[i + 3][1] == 1 && (s[i] != s[i + 3] || s[i + 1] != s[i + 4] || s[i + 2] != s[i + 5])) can[i][1] = 1; } for (int i = 5; i < n; i++) { if (can[i][0] == 1) { string t = ""; t += s[i]; t += s[i + 1]; ans.insert(t); } if (can[i][1] == 1) { string t = ""; t += s[i]; t += s[i + 1]; t += s[i + 2]; ans.insert(t); } } cout << ans.size() << endl; for (auto 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; long long power(long long a, long long b, long long md) { return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md)); } const int xn = 1e4 + 10; const int xm = -20 + 10; const int sq = 320; const int inf = 1e9 + 10; const long long INF = 1e18 + 10; const int mod = 998244353; const int base = 257; int n; string s, last[2][xn], s1, s2; set<string> st; bool dp[2][xn]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> s; n = int(s.size()); dp[0][n] = dp[1][n] = true; for (int i = n - 1; i >= 0; --i) { for (int j = i; j < min(n, i + 2); ++j) last[0][i] += s[j]; for (int j = i; j < min(n, i + 3); ++j) last[1][i] += s[j]; if (int(last[0][i].size()) == 2) { if (dp[0][i + 2] && last[0][i] != last[0][i + 2]) dp[0][i] = true; if (dp[1][i + 2]) dp[0][i] = true; } if (int(last[1][i].size()) == 3) { if (dp[0][i + 3]) dp[1][i] = true; if (dp[1][i + 3] && last[1][i] != last[1][i + 3]) dp[1][i] = true; } if (i > 4) { if (dp[0][i]) st.insert(last[0][i]); if (dp[1][i]) st.insert(last[1][i]); } } cout << int(st.size()) << '\n'; for (string x : st) 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; int valid[10005]; string s; set<string> comb; int main() { cin >> s; if (s.size() < 7) { puts("0"); return 0; } string u; u = s.substr(((int)s.size()) - 2, 2); comb.insert(u); valid[((int)s.size()) - 2] += 2; if (((int)s.size()) >= 8) { u = s.substr(((int)s.size()) - 3, 3); comb.insert(u); valid[((int)s.size()) - 3] += 3; } for (int i = ((int)s.size()) - 4; i > 4; i--) { if (valid[i + 2] == 3 || valid[i + 2] == 5 || (valid[i + 2] == 2 && (s[i] != s[i + 2] || s[i + 1] != s[i + 3]))) { valid[i] += 2; u = s.substr(i, 2); comb.insert(u); } if (valid[i + 3] == 2 || valid[i + 3] == 5 || (valid[i + 3] == 3 && (s[i] != s[i + 3] || s[i + 1] != s[i + 4] || s[i + 2] != s[i + 5]))) { valid[i] += 3; u = s.substr(i, 3); comb.insert(u); } } cout << comb.size() << endl; for (set<string>::iterator it = comb.begin(); it != comb.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; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } const long long maxn = (long long)4e5 + 100; const long long maxm = (long long)52; const int logn = 22; const long long INF = 1e9 + 100; const long long MOD = 1000000007; const double PI = acos(-1.0); const double EPS = 1e-12; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string str; while (cin >> str) { reverse((str).begin(), (str).end()); set<string> dic; str.resize(str.size() - 5); int len = str.size(); vector<int> dp2(len + 1); vector<int> dp3(len + 1); dp2[0] = 1; dp3[0] = 1; for (int i = 1; i <= len; ++i) { if (i >= 2 && dp2[i - 2]) { if (i < 4 || str.substr(i - 2, 2) != str.substr(i - 4, 2)) { dp2[i] |= dp2[i - 2]; dic.insert(str.substr(i - 2, 2)); } } if (i >= 2 && dp3[i - 2]) { dp2[i] |= dp3[i - 2]; dic.insert(str.substr(i - 2, 2)); } if (i >= 3 && dp2[i - 3]) { dp3[i] |= dp2[i - 3]; dic.insert(str.substr(i - 3, 3)); } if (i >= 3 && dp3[i - 3]) { if (i < 6 || str.substr(i - 3, 3) != str.substr(i - 6, 3)) { dp3[i] |= dp3[i - 3]; dic.insert(str.substr(i - 3, 3)); } } } vector<string> ans((dic).begin(), (dic).end()); for (auto& x : ans) { reverse((x).begin(), (x).end()); } sort((ans).begin(), (ans).end()); cout << ans.size() << endl; for (auto x : ans) { 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; string s; set<string> S; bool dp2[10005], dp3[10005], dp[10005]; int main() { cin >> s; dp[s.size()] = 1; dp[s.size() - 1] = 0; for (int i = s.size() - 2; i >= 0; i--) { int x = s.size() - i; if (x >= 2) { char a = s[i], b = s[i + 1]; if (i + 3 >= s.size()) { dp2[i] |= dp[i + 2]; } else if (a != s[i + 2] || b != s[i + 3]) { dp2[i] |= dp[i + 2]; } else { dp2[i] |= dp3[i + 2]; } } if (x >= 3) { char a = s[i], b = s[i + 1], c = s[i + 2]; if (i + 5 >= s.size()) { dp3[i] |= dp[i + 3]; } else if (a != s[i + 3] || b != s[i + 4] || c != s[i + 5]) { dp3[i] |= dp[i + 3]; } else { dp3[i] |= dp2[i + 3]; } } dp[i] |= dp2[i]; dp[i] |= dp3[i]; } for (int i = s.size() - 1; i >= 5; i--) { { if (dp2[i]) { string q = ""; for (int x = i; x < i + 2; x++) q.push_back(s[x]); S.insert(q); } if (dp3[i]) { string q = ""; for (int x = i; x < i + 3; x++) q.push_back(s[x]); S.insert(q); } } } cout << S.size() << endl; 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
#include <bits/stdc++.h> using namespace std; int n; char ipt[10010]; bool dt[10005][2]; vector<string> ans; int main() { scanf("%s", ipt + 1); n = strlen(ipt + 1); dt[n - 1][0] = true; dt[n - 2][1] = true; for (int i = n - 3; i > 5; i--) { for (int k = 0; k < 2; k++) { if (dt[i + k + 2][k ^ 1]) { dt[i][k] = true; continue; } if (!dt[i + k + 2][k]) continue; bool flag = true; for (int j = 0; j < k + 2; j++) { if (ipt[i + j] != ipt[i + k + 2 + j]) { flag = false; break; } } if (!flag) dt[i][k] = true; } } for (int i = n - 1; i > 5; i--) { for (int k = 0; k < 2; k++) { if (!dt[i][k]) continue; string tmp; for (int j = 0; j < k + 2; j++) tmp += ipt[i + j]; ans.push_back(tmp); } } sort(ans.begin(), ans.end()); ans.erase(unique(ans.begin(), ans.end()), ans.end()); printf("%d\n", 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; int muzu[10005][2]; int main(void) { int N; char Pole[10005]; int i; for (i = 0;; i++) { scanf("%c", &Pole[i]); if (Pole[i] == '\n') { N = i; break; } muzu[i][0] = 0; muzu[i][1] = 0; } muzu[N + 1][0] = 0; muzu[N + 1][1] = 0; vector<int> cisla; muzu[N][0] = 1; muzu[N][1] = 1; muzu[N - 2][1] = 1; for (i = N - 2; i > 4; i--) { if (muzu[i + 2][0] == 1 || (muzu[i + 3][1] == 1 && ((Pole[i - 1] != Pole[i + 2]) || (Pole[i - 2] != Pole[i + 1]) || (Pole[i - 3] != Pole[i])))) muzu[i][1] = 1; if (muzu[i + 3][1] == 1 || (muzu[i + 2][0] == 1 && ((Pole[i - 1] != Pole[i + 1]) || (Pole[i - 2] != Pole[i])))) muzu[i][0] = 1; } for (i = 0; i <= N; i++) { if (muzu[i][0] == 1 && i >= 7) { cisla.push_back(256 * 256 * Pole[i - 2] + 256 * Pole[i - 1]); } if (muzu[i][1] == 1 && i >= 8) { cisla.push_back(256 * 256 * Pole[i - 3] + 256 * Pole[i - 2] + Pole[i - 1]); } } int res = 0; sort(cisla.begin(), cisla.end()); for (i = 0; i < cisla.size(); i++) { if (i == 0) res++; else if (cisla[i] != cisla[i - 1]) res++; } printf("%i\n", res); int x; if (res == 0) return 0; for (i = 0; i < cisla.size(); i++) { if (i == 0 || cisla[i] != cisla[i - 1]) { x = cisla[i]; printf("%c", x / (256 * 256)); x %= (256 * 256); printf("%c", x / 256); x %= 256; if (x != 0) { printf("%c", x); } 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
import java.io.*; import java.util.*; public final class reberland { static FastScanner sc=new FastScanner(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out=new PrintWriter(System.out); static long mod1=1000000000+7,mod2=1000000000+9,base=37; static long[] pow1,pow2; public static void main(String args[]) throws Exception { char[] a=sc.next().toCharArray(); pow1=new long[3];pow2=new long[3]; pow1[0]=pow2[0]=1; for(int i=1;i<3;i++) { pow1[i]=(pow1[i-1]*base)%mod1; pow2[i]=(pow2[i-1]*base)%mod2; } Pair[][] res=new Pair[a.length][3]; for(int i=a.length-1;i>=6;i--) { long hash1=0,hash2=0; for(int j=i-1,k=0;j<=i;j++,k++) { hash1=hash1+((a[j]-'a'+1)*pow1[k])%mod1; hash1=(hash1%mod1+mod1)%mod1; hash2=hash2+((a[j]-'a'+1)*pow2[k])%mod2; hash2=(hash2%mod2+mod2)%mod2; } res[i][1]=new Pair(hash1,hash2); } for(int i=a.length-1;i>=7;i--) { long hash1=0,hash2=0; for(int j=i-2,k=0;j<=i;j++,k++) { hash1=hash1+((a[j]-'a'+1)*pow1[k])%mod1; hash1=(hash1%mod1+mod1)%mod1; hash2=hash2+((a[j]-'a'+1)*pow2[k])%mod2; hash2=(hash2%mod2+mod2)%mod2; } res[i][2]=new Pair(hash1,hash2); } List<Pair> list=new ArrayList<Pair>(); boolean[][] v=new boolean[a.length][3]; v[a.length-1][1]=v[a.length-1][2]=true; if(a.length>=7) { list.add(new Pair(a.length-1,1)); } if(a.length>=8) { list.add(new Pair(a.length-1,2)); } for(int i=a.length-3;i>=5;i--) { if(true) { if(i-1>=5) { if(i+3<a.length && v[i+3][2]) { list.add(new Pair(i,1)); v[i][1]=true; } else if(i+2<a.length && v[i+2][1] && (res[i][1].hash1!=res[i+2][1].hash1 || res[i][1].hash2!=res[i+2][1].hash2)) { list.add(new Pair(i,1)); v[i][1]=true; } } if(i-2>=5) { if(i+2<a.length && v[i+2][1]) { list.add(new Pair(i,2)); v[i][2]=true; } else if(i+3<a.length && v[i+3][2] && (res[i][2].hash1!=res[i+3][2].hash1 || res[i][2].hash2!=res[i+3][2].hash2)) { list.add(new Pair(i,2)); v[i][2]=true; } } } } Set<String> s1=new HashSet<String>(); for(Pair p:list) { if(p.hash2==1) { int curr=(int)p.hash1; s1.add(new String(a,curr-1,2)); } else { int curr=(int)p.hash1; s1.add(new String(a,curr-2,3)); } } List<String> al=new ArrayList<String>(); for(String s:s1) { al.add(s); } Collections.sort(al); out.println(al.size()); for(String s:al) { out.println(s); } out.close(); } } class Pair { long hash1,hash2; public Pair(long hash1,long hash2) { this.hash1=hash1; this.hash2=hash2; } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public String next() throws Exception { return nextToken().toString(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
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 = 10005; int N; string S; bool dp[MAXN][4]; set<string> ans; bool check(int idx, int len) { if (idx + 2 * len > N) return false; for (int i = 0; i < len; i++) if (S[idx + i] != S[idx + len + i]) return true; return false; } int main() { ios::sync_with_stdio(0); cin >> S; N = S.size(); dp[N][2] = dp[N][3] = true; for (int i = N - 1; i >= 5; i--) { dp[i][2] = (dp[i + 2][2] && check(i, 2)) || dp[i + 2][3]; dp[i][3] = (dp[i + 3][3] && check(i, 3)) || dp[i + 3][2]; } for (int i = 5; i < N; i++) { if (dp[i][2]) { string s = ""; s += S[i]; s += S[i + 1]; ans.insert(s); } if (dp[i][3]) { string s = ""; s += S[i]; s += S[i + 1]; s += S[i + 2]; ans.insert(s); } } cout << ans.size() << "\n"; for (set<string>::iterator 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; bool dp[10010][2]; string ss[10010]; int main(void) { std::ios::sync_with_stdio(false); string s; cin >> s; int len = s.length(); set<string> res; if (len > 6) dp[len - 2][0] = 1, ss[len - 2] = s.substr(len - 2, 2), res.insert(ss[len - 2]); if (len > 7) dp[len - 3][1] = 1, ss[len - 3] = s.substr(len - 3, 3), res.insert(ss[len - 3]); for (int i = len - 4; i > 4; i--) { if (dp[i + 2][1] || (dp[i + 2][0] && ss[i + 2] != s.substr(i, 2))) { dp[i][0] = 1; ss[i] = s.substr(i, 2); res.insert(ss[i]); } if (dp[i + 3][0] || (dp[i + 3][1] && ss[i + 3] != s.substr(i, 3))) { dp[i][1] = 1; ss[i] = s.substr(i, 3); res.insert(ss[i]); } } cout << res.size() << endl; for (set<string>::iterator it = res.begin(); it != res.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.Set; import java.util.TreeSet; public class Reberland_Linguistics_new { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str = scan.nextLine(); Set<String> set=new TreeSet<String>(); set=doTheWork(str.length(),str,set); System.out.println(set.size()); for(String s:set) { System.out.println(s); } } private static Set<String> doTheWork(int i, String str, Set<String> set) { boolean len2[]=new boolean[str.length()+11]; boolean len3[]=new boolean[str.length()+11]; len2[str.length()]=len3[str.length()]=true; for(i=i-2;i>4;i--) { if( len3[i+2] || ( len2[i+2] && !str.substring(i, i+2).equals(str.substring(i+2, i+4))) ) { set.add(str.substring(i, i+2)); len2[i]=true; } if( len2[i+3] || ( len3[i+3] && !str.substring(i, i+3).equals(str.substring(i+3, i+6))) ) { set.add(str.substring(i, i+3)); len3[i]=true; } } return set; } }
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.*; public class suffix { public static void main(String[] args) { Scanner sc=new Scanner (System.in); String str=sc.nextLine(); str=str.substring(5); boolean[] fix=new boolean[26*26*27]; boolean[][] check=new boolean[str.length()+1][2]; for (int n=str.length(); n>1; n--) { if (n==str.length()) { fix[Suf2Num(str.substring(n-2,n))]=true; check[n][0]=true; if (n>2) { fix[Suf2Num(str.substring(n-3,n))]=true; check[n][1]=true; } } else { if (n<=str.length()-2) { if (check[n+2][0]) { String two=str.substring(n-2,n); if (!two.equals(str.substring(n,n+2))) { fix[Suf2Num(two)]=true; check[n][0]=true; } if (n>2) { String three=str.substring(n-3,n); fix[Suf2Num(three)]=true; check[n][1]=true; } } } if (n<=str.length()-3) { if (check[n+3][1]) { String two=str.substring(n-2,n); fix[Suf2Num(two)]=true; check[n][0]=true; if (n>2) { String three=str.substring(n-3,n); if (!three.equals(str.substring(n,n+3))) { fix[Suf2Num(three)]=true; check[n][1]=true; } } } } } } int ans=0; for (int n=0; n<26*26*27; n++) { if (fix[n]) ans++; } System.out.println(ans); for (int n=0; n<26*26*27; n++) { if (fix[n]) System.out.println(Num2Suf(n)); } } public static int LowLet2Num(char c) { //Convert a lower-case letter to a number (e.g. 'b'->2) return (int)c-97; } public static char Num2Let(int n) { return (char)(n+97); } public static int Suf2Num(String s) { int ans=0; ans+=LowLet2Num(s.charAt(0))*26*27; ans+=LowLet2Num(s.charAt(1))*27; if (s.length()>2) ans+=(LowLet2Num(s.charAt(2))+1); return ans; } public static String Num2Suf(int n) { int d1=n/(26*27); n=n%(26*27); int d2=n/(27); n=n%27; String ans=Num2Let(d1)+""+Num2Let(d2); if (n>0) ans=ans+Num2Let(n-1); return ans; } }
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; void deba(int* a, int n) { cerr << "| "; for (int i = 0; i < (n); i++) cerr << a[i] << " "; cerr << "|" << endl; } inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } const int INF = 1 << 30; char s[12345]; vector<string> out; vector<string> ou; bool en(int k, int len) { if (len - k == 1) return false; if (len - k == 4) { if (s[k] == s[k + 2] && s[k + 1] == s[k + 3]) return false; } if (len - k == 6) { if (s[k] == s[k + 2] && s[k + 1] == s[k + 3] && s[k + 2] == s[k + 4] && s[k + 3] == s[k + 5] && s[k] == s[k + 3] && s[k + 1] == s[k + 4] && s[k + 2] == s[k + 5]) return false; } for (int i = (k); i <= (len - 1); i++) if (s[i] != s[k]) return true; int f = len - k; int x = 9; while (x < 10010) { if (f == x) return false; x += 2; if (f == x) return false; x += 3; } return true; } int main() { scanf("%s", s); int len = strlen(s); cout.sync_with_stdio(false); if (len <= 6) { cout << 0 << endl; return 0; } for (int i = (5); i <= (len - 1); i++) { if (i + 1 <= len - 1 && i + 1 != len - 2) { if (en(i + 2, len)) { string s1 = ""; s1 += s[i]; s1 += s[i + 1]; out.push_back(s1); } } if (i + 2 <= len - 1 && i + 2 != len - 2) { if (en(i + 3, len)) { string s1 = ""; s1 += s[i]; s1 += s[i + 1]; s1 += s[i + 2]; out.push_back(s1); } } } sort(out.begin(), out.end()); ou.push_back(out[0]); for (int i = (1); i <= ((int)out.size() - 1); i++) { if (out[i] != out[i - 1]) ou.push_back(out[i]); } cout << ou.size() << endl; for (int i = 0; i < (ou.size()); i++) cout << ou[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; long long pwr(long long base, long long p, long long mod = (1000000007LL)) { long long ans = 1; while (p) { if (p & 1) ans = (ans * base) % mod; base = (base * base) % mod; p /= 2; } return ans; } string str; bool allowed[100025][5]; int n; set<string> ans; int main() { ios_base::sync_with_stdio(0); cin >> str; int n = str.length(); allowed[n][2] = allowed[n][3] = true; for (int i = n - 1; i >= 5; i--) { if (allowed[i + 2][3]) allowed[i][2] = true; if (i + 3 < n && str.substr(i, 2) != str.substr(i + 2, 2) && allowed[i + 2][2]) allowed[i][2] = true; if (allowed[i + 3][2]) allowed[i][3] = true; if (i + 5 < n && str.substr(i, 3) != str.substr(i + 3, 3) && allowed[i + 3][3]) allowed[i][3] = true; } for (int i = 5; i < n; i++) { if (allowed[i][2]) ans.insert(str.substr(i, 2)); if (allowed[i][3]) ans.insert(str.substr(i, 3)); } cout << (int)ans.size() << endl; for (auto s : ans) cout << s << 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 ReberlandLinguistics { Scanner sc = new Scanner(System.in); private String s; private int n; private boolean[][] v; private boolean[][] m; private TreeSet<String> set; public ReberlandLinguistics(String s) { this.s = s; this.n = this.s.length(); this.set = new TreeSet<>(); this.v = new boolean[this.n + 5][5]; this.m = new boolean[this.n + 5][5]; for (int i = 5; i < this.n; i++) { evaluar(i, 2); evaluar(i, 3); } System.out.println(this.set.size()); for (String str : this.set) { System.out.println(str); } } boolean evaluar(int i, int j) { if (i + j > this.n) { return false; } if (i + j == this.n) { this.set.add(this.s.substring(i, i + j)); return true; } if (this.v[i][j]) return this.m[i][j]; this.v[i][j] = true; boolean r = false; int nxt = i + j; if (j == 2) { if (evaluar(nxt, 3) || (nxt + 2 <= this.n && !this.s.substring(i, i + 2).equals(this.s.substring(nxt, nxt + 2)) && evaluar(nxt, 2))) { r = true; } } else { if (evaluar(nxt, 2) || (nxt + 3 <= this.n && !this.s.substring(i, i + 3).equals(this.s.substring(nxt, nxt + 3)) && evaluar(nxt, 3))) { r = true; } } if (r) { this.set.add(this.s.substring(i, i + j)); } return this.m[i][j] = r; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String palabra = sc.nextLine(); new ReberlandLinguistics(palabra); } }
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.Scanner; import java.util.TreeSet; public class ReberlandLinguistics { 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[] valid2 = new boolean[n]; //validated from 2 boolean[] valid3 = new boolean[n]; valid2[n-1] = true; valid3[n-1] = true; boolean debug = false; for(int i = n-1; i >= 5; i--) { if(debug)System.out.println("i " + i); if(!valid2[i] && !valid3[i])continue; //try 2 if(i >= 6) { if(!wrong2[i] || ((i+3 <= n-1) && valid3[i]) ) { if(i-2>0)valid2[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"); valid2[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) && valid2[i+2]) ) { if(i-3>0)valid3[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"); } valid3[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
#include <bits/stdc++.h> using namespace std; const int MaxN = 1e5 + 10; vector<string> tot[MaxN]; 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 (set<string>::iterator ite = ans.begin(); ite != ans.end(); ite++) { 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
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ //System.out.println(); public class Solution { public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); int len = s.length(); if(len <= 6) { System.out.println("0"); return; } s = s.substring(5, len); len -= 5; int dp[] = new int[len]; for(int i = 0; i < 4; i++) { if(len - 1 - i < 0) continue; dp[len - 1 - i] = i == 1 ? 0 : 1; } for(int i = len - 5; i >= 0; i--) { String temp1 = ""; String temp2 = ""; if(i + 4 < len && dp[i + 4] != 0) { temp1 = s.substring(i + 1, i + 3); temp2 = s.substring(i + 3, i + 5); if(!temp1.equals(temp2)) { //System.out.println(temp1 + " " + temp2); dp[i] = 1; continue; } } if(i + 5 < len && dp[i + 5] != 0) { dp[i] = 1; continue; } if(i + 6 < len && dp[i + 6] != 0) { temp1 = s.substring(i + 1, i + 4); temp2 = s.substring(i + 4, i + 7); if(!temp1.equals(temp2)) { dp[i] = 1; continue; } } } TreeSet ans = new TreeSet(); // for(int i = 0; i < len; i++) // System.out.print(dp[i] + " "); for(int i = len - 1; i >= 1; i--) { if(i == len - 2) continue; if(dp[i] == 1) ans.add(s.substring(i - 1, i + 1)); } for(int i = len - 1; i >= 2; i--) { if(i == len - 2) continue; if(dp[i] == 1) ans.add(s.substring(i - 2, i + 1)); } System.out.println(ans.size()); Iterator itr = ans.iterator(); while(itr.hasNext()) { System.out.println(itr.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 dp[500005]; int main() { string a, ans; set<string> s; cin >> a; int len = a.length(); dp[len] = 1; for (int i = len - 1; i > 4; i--) { for (int l = 2; l <= 3; l++) { if (dp[i + l]) { ans = a.substr(i, l); if (a.find(ans, i + l) != i + l || dp[i + 5]) { s.insert(ans); dp[i] = 1; } } } } printf("%d\n", s.size()); for (set<string>::iterator its = s.begin(); its != s.end(); ++its) { cout << *its << "\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; char ss[11111]; string s; int n; set<string> used; int can2[11111]; int can3[11111]; int main() { scanf("%s", ss); s = ss; n = (int)(s).size(); can2[n - 2] = 1; can3[n - 3] = 1; for (int i = n - 4; i >= 5; --i) { if (((s[i] != s[i + 2] || s[i + 1] != s[i + 3]) && can2[i + 2]) || can3[i + 2]) { can2[i] = 1; } if (((s[i] != s[i + 3] || s[i + 1] != s[i + 4] || s[i + 2] != s[i + 5]) && can3[i + 3]) || can2[i + 3]) { can3[i] = 1; } } for (int i = 5; i < n; ++i) { if (can2[i]) used.insert(s.substr(i, 2)); if (can3[i]) used.insert(s.substr(i, 3)); } printf("%d\n", (int)(used).size()); for (set<string>::iterator it = used.begin(); it != used.end(); ++it) { printf("%s\n", (*it).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
lectura = input() sufix = set() comb = {(len(lectura), 2)} setPrueba = set() while comb: x, y = comb.pop() pos3 = x + y for i in [y, 5 - y]: posIni = x - i stringActual = (posIni, i) if ( stringActual in setPrueba or (posIni < 5) or (lectura[posIni:x] == lectura[x:pos3]) ): #print("encontrado en el set") continue else: sufix.add(lectura[posIni:x]) comb.add(stringActual) setPrueba.add(stringActual) conclusion = sorted(sufix) print(len(sufix)) for i in range(0,len(conclusion)): print(conclusion[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
from sys import * setrecursionlimit(20000) dp = [] ans = [] def fun(s, pos, r, ln): if pos <= 4+ln: return 0 if dp[pos][ln-2] != 0: return dp[pos][ln-2] if s[pos-ln:pos] != r: dp[pos][ln-2] = 1 + fun(s, pos - ln, s[pos-ln:pos],2) + fun(s, pos - ln, s[pos-ln:pos],3) ans.append(s[pos-ln:pos]) ''' if pos > 4+ln and s[pos-3:pos] != r: dp[pos][1] = 1 + fun(s, pos - 3, s[pos-3:pos]) ans.append(s[pos-3:pos])''' return dp[pos][ln-2] s = input() dp = [[0, 0] for i in range(len(s) + 1)] fun(s, len(s), '', 2) fun(s, len(s), '', 3) ans = list(set(ans)) ans.sort() print (len(ans)) for i in ans: 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
d = {} data = raw_input() l = len(data) if l <= 6: print 0 else: dp = [[0 for x in xrange(4)] for y in xrange(l)] dp[l - 2][2] = 1 d[data[-2:]] = 1 if l > 7: dp[l - 3][3] = 1 d[data[-3:]] = 1 for i in xrange(l - 2, 5, -1): for j in xrange(2, 4): if dp[i][j] == 1: if i - 2 >= 5 and data[i - 2:i] != data[i:i+j]: dp[i - 2][2] = 1 d[data[i - 2:i]] = 1 if i - 3 >= 5 and data[i - 3:i] != data[i:i + j]: dp[i - 3][3] = 1 d[data[i - 3:i]] = 1 print len(d) print '\n'.join(sorted(d.keys()))
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 sys sys.setrecursionlimit(100000) '''def query(curr,lasti): global s if curr==len(s):return 1 if dp[curr][lasti]!=-1:return dp[curr][lasti] last=s[curr-(lasti+2):curr] ans=0 for i in xrange(2,4): if curr+i<=len(s): u=s[curr:curr+i] if u!=last: ans=ans|query(curr+i,i-2) dp[curr][lasti]=ans return ans''' s=raw_input().strip() dp=[[-1 for i in xrange(2)]for j in xrange(len(s)+1)] for curr in xrange(len(s),-1,-1): for lasti in xrange(2): if curr==len(s): dp[curr][lasti]=1 continue last=s[curr-(lasti+2):curr] ans=0 for i in xrange(2,4): if curr+i<=len(s): u=s[curr:curr+i] if u!=last: ans=ans|dp[curr+i][i-2] dp[curr][lasti]=ans out=[] for i in xrange(5,len(s)): for j in xrange(2,4): if i+j<=len(s) and dp[i+j][j-2]: out.append(s[i:i+j]) out=sorted(set(out)) print len(out) for i in sorted(set(out)):print i
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; const int MAXN = 100000 + 5; const int MAXM = 26 + 3; char str[MAXN]; int treecnt; string out[MAXN]; int vis2[MAXN], vis3[MAXN]; int len; bool check(int st, int l) { int ok = 0; if (st + 2 * l > len) { if (st + 2 * l != len + 1) return false; return true; } for (int i = st; i < st + l; i++) { if (str[i] != str[i + l]) return true; } return false; } int main() { std::ios::sync_with_stdio(false); scanf("%s", str); len = strlen(str); int cnt = 0; memset(vis2, 0, sizeof vis2); memset(vis3, 0, sizeof vis3); vis2[len] = 1; vis3[len] = 1; for (int i = len - 1; i >= 0; i--) { if (i <= 4) break; int ok = 0; int t1, t2; t1 = t2 = 0; if ((vis3[i + 2] || (vis2[i + 2] && check(i, 2)))) t1 = 1; if ((vis2[i + 3] || (vis3[i + 3] && check(i, 3)))) t2 = 1; if (t1) { out[cnt] = ""; for (int j = i; j < i + 2; j++) out[cnt] += str[j]; cnt++; vis2[i] = 1; } if (t2) { out[cnt] = ""; for (int j = i; j < i + 3; j++) out[cnt] += str[j]; cnt++; vis3[i] = 1; } } int ans = 0; sort(out, out + cnt); for (int i = 1; i < cnt; i++) if (out[i] != out[i - 1]) ans++; cout << ans + (cnt > 0) << endl; for (int i = 0; i < cnt; i++) { if (i == 0 || out[i] != out[i - 1]) cout << out[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 HashVal(const string& S) { int h = 0; for (int i = 0; i < S.size(); i++) { h = h * 27 + S[i] - 'a' + 1; } return h; } map<pair<int, int>, bool> hashed; string S; int dp[11111][5]; bool Can(int prevpos, int pos) { int L = S.size(); if (pos == L) return true; if (dp[pos][pos - prevpos] != -1) return dp[pos][pos - prevpos]; string prevstring = S.substr(prevpos, pos - prevpos); bool answer = false; for (int j = pos + 1; j <= min(L - 1, pos + 2); j++) { if (j - pos + 1 < 2) continue; string curr = S.substr(pos, j - pos + 1); if (curr == prevstring) continue; answer |= Can(pos, j + 1); } return dp[pos][pos - prevpos] = answer; } void Solve() { int L = S.size(); for (int i = 0; i <= L; i++) for (int j = 0; j < 5; j++) dp[i][j] = -1; for (int pos = L; pos >= 0; pos--) { for (int prevpos = pos - 2; prevpos >= max(0, pos - 3); prevpos--) { if (pos == L) { dp[pos][pos - prevpos] = 1; continue; } if (pos - prevpos < 2) { dp[pos][pos - prevpos] = 0; continue; } string prevstring = S.substr(prevpos, pos - prevpos); int answer = 0; for (int j = pos + 1; j <= min(L - 1, pos + 2); j++) { if (j - pos + 1 < 2) continue; string curr = S.substr(pos, j - pos + 1); if (curr == prevstring) continue; answer |= dp[j + 1][j + 1 - pos]; cout << j + 1 - pos << endl; } dp[pos][pos - prevpos] = answer; } } } int main() { cin >> S; int L = S.size(); long long hashval = 0; for (int i = 0; i <= L; i++) for (int j = 0; j < 5; j++) dp[i][j] = -1; set<string> answers; for (int i = 5; i < L; i++) { for (int j = i + 1; j <= min(L - 1, i + 2); j++) { if (j - i + 1 < 2) continue; string prev = S.substr(i, j - i + 1); if (Can(i, j + 1)) answers.insert(prev); } } cout << answers.size() << endl; for (set<string>::iterator it = answers.begin(); it != answers.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
#include <bits/stdc++.h> const int MAXN = 1e5 + 5; std::string str; int n; int f[MAXN][2]; int main() { std::ios::sync_with_stdio(false); std::cin >> str; int n = str.length(); if (n <= 5) { puts("0"); return 0; } f[n + 1][0] = f[n + 1][1] = 1; for (int i = n; i >= 6; --i) { for (int j = 0; j <= 1; ++j) { if (j == 0) { f[i][j] |= (f[i + 2][0] & (!(str[i - 1] == str[i + 2 - 1] && str[i + 1 - 1] == str[i + 3 - 1]))) | (f[i + 2][1]); } else { f[i][j] |= (f[i + 3][0]) | (f[i + 3][1] & (!(str[i - 1] == str[i + 3 - 1] && str[i + 1 - 1] == str[i + 4 - 1] && str[i + 2 - 1] == str[i + 5 - 1]))); } } } std::set<std::string> S; for (int i = 6; i <= n; ++i) { if (f[i][0]) S.insert(str.substr(i - 1, 2)); if (f[i][1]) S.insert(str.substr(i - 1, 3)); } std::cout << S.size() << '\n'; for (auto x : S) std::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; long long rdtsc() { long long tmp; asm("rdtsc" : "=A"(tmp)); return tmp; } inline int myrand() { return abs((rand() << 15) ^ rand()); } inline int rnd(int x) { return myrand() % x; } void precalc() {} const int maxn = 1e4 + 10; int n; char s[maxn]; bool read() { if (scanf("%s", s) < 1) { return false; } n = strlen(s); return true; } set<string> ans; int dp[maxn][2]; void solve() { ans.clear(); memset(dp, 0, sizeof(dp)); if (n >= 7) { dp[n - 2][0] = 1; } if (n >= 8) { dp[n - 3][1] = 1; } for (int i = n - 2; i >= 5; --i) { for (int it = 0; it < 2; ++it) { int len = it + 2; if (!dp[i][it]) { continue; } string cur = string(s + i, len); ans.insert(cur); for (int nit = 0; nit < 2; ++nit) { int nlen = nit + 2; if (i - nlen < 5) { break; } string t = string(s + (i - nlen), nlen); if (t != cur) { dp[i - nlen][nit] = 1; } } } } printf("%d\n", ((int)(ans).size())); for (auto t : ans) { printf("%s\n", t.c_str()); } } int main() { srand(rdtsc()); precalc(); while (true) { if (!read()) { break; } 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 java.io.*; import java.math.BigInteger; import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.*; public class maind2 { static double eps=(double)1e-6; static long mod=(int)1e9+7; public static void main(String args[]){ InputReader in = new InputReader(System.in); OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); //----------My Code---------- String s=in.nextLine(); char a[]=new char[s.length()+5]; int n=s.length(); for(int i=0;i<n;i++){ a[5+i]=s.charAt(n-i-1); } a[0]='*';a[1]='*';a[2]='*';a[3]='*';a[4]='*'; s=new String(a); boolean v2[]=new boolean[n+5]; boolean v3[]=new boolean[n+5]; v2[4]=v3[4]=true; for(int i=1+5;i<n-5+5;i++){ if(v3[i-2]){ v2[i]=true; } if(v2[i-2]){ String chk1=s.substring(i-3,i-1); String chk2=s.substring(i-1,i+1); if(!chk1.equals(chk2)){ v2[i]=true; } } if(v2[i-3]){ v3[i]=true; } if(v3[i-3]){ String chk1=s.substring(i-5,i-2); String chk2=s.substring(i-2,i+1); if(!chk1.equals(chk2)){ v3[i]=true; } } } TreeSet<String> tree=new TreeSet<String>(); for(int i=5;i<n;i++){ if(v2[i]){ String mystr=""+s.charAt(i)+s.charAt(i-1); tree.add(mystr); } if(v3[i]){ String mystr=""+s.charAt(i)+s.charAt(i-1)+s.charAt(i-2); tree.add(mystr); } } //out.println(s); out.println(tree.size()); for(String x:tree){ out.println(x); } out.close(); //---------------The End------------------ } static public int f(int x,int c[],int k) { int ans=0; for(int i=0;i<26;i++){ if(c[i]==0) continue; if(c[i]<x){ ans+=(c[i]); } if(c[i]>x+k){ ans+=(c[i]-(x+k)); } } return ans; } static long modulo(long a,long b,long c) { long x=1; long y=a; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return x%c; } static long gcd(long x, long y) { long r=0, a, b; a = (x > y) ? x : y; // a is greater number b = (x < y) ? x : y; // b is smaller number r = b; while(a % b != 0) { r = a % b; a = b; b = r; } return r; } static class Pair implements Comparable<Pair>{ int x; int y; int i; Pair(int xx,int yy){ x=xx; y=yy; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream inputstream) { reader = new BufferedReader(new InputStreamReader(inputstream)); tokenizer = null; } public String nextLine(){ String fullLine=null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine=reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(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; set<string> out; string in; set<pair<int, string> > visited; void DP(int pos, string prev) { if (pos <= 5) return; pair<int, string> tmp = make_pair(pos, prev); if (visited.find(tmp) != visited.end()) return; visited.insert(tmp); string s = ""; if (pos >= 6) { s = in.substr(pos - 1, 2); if (s != prev) { out.insert(s); DP(pos - 2, s); } if (pos >= 7) { s = in[pos - 2] + s; if (s != prev) { out.insert(s); DP(pos - 3, s); } } } } int main() { cin >> in; DP(in.size() - 1, ""); cout << out.size() << "\n"; for (auto it = out.begin(); it != out.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; signed main() { string s; cin >> s; int n = (int)s.size(); vector<vector<bool>> dp(n, vector<bool>(2)); dp[n - 1][0] = (n >= 7); dp[n - 1][1] = (n >= 8); for (int i = n - 3; i >= 6; --i) { string sub2 = s.substr(i - 1, 2); string sub3 = s.substr(i - 2, 3); if (i <= n - 3) { string psub2 = s.substr(i + 1, 2); if (psub2 != sub2 && dp[i + 2][0]) dp[i][0] = 1; } if (i <= n - 4) { if (dp[i + 3][1]) dp[i][0] = 1; } if (i >= 7) { if (i <= n - 3) { if (dp[i + 2][0]) dp[i][1] = 1; } if (i <= n - 4) { string psub3 = s.substr(i + 1, 3); if (psub3 != sub3 && dp[i + 3][1]) dp[i][1] = 1; } } } set<string> ans; for (int i = 6; i < n; ++i) { if (dp[i][0]) ans.insert(s.substr(i - 1, 2)); if (dp[i][1]) ans.insert(s.substr(i - 2, 3)); } 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
import java.util.Scanner; import java.util.TreeSet; public class ReberlandLinguistic { Scanner sc = new Scanner(System.in); String s; int n; boolean[][] v; boolean[][] memo; TreeSet<String> set; void run() { s = sc.next(); n = s.length(); set = new TreeSet<>(); v = new boolean[n + 5][5]; memo = new boolean[n + 5][5]; for (int i = 5; i < n; i++) { dfs(i, 2); dfs(i, 3); } System.out.println(set.size()); for (String s : set) System.out.println(s); } boolean dfs(int i, int j) { if (i + j > n) return false; if (i + j == n) { set.add(s.substring(i, i + j)); return true; } if (v[i][j]) return memo[i][j]; v[i][j] = true; boolean ret = false; int nextP = i + j; if (j == 2) { if (dfs(nextP, 3) || (nextP + 2 <= n && !s.substring(i, i + 2).equals(s.substring(nextP, nextP + 2)) && dfs(nextP, 2))) ret = true; } else { if (dfs(nextP, 2) || (nextP + 3 <= n && !s.substring(i, i + 3).equals(s.substring(nextP, nextP + 3)) && dfs(nextP, 3))) ret = true; } if (ret) set.add(s.substring(i, i + j)); return memo[i][j] = ret; } public static void main(String[] args) { new ReberlandLinguistic().run(); } }
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 int INF = (long long int)1e9 + 10; const long long int INFLL = (long long int)1e18 + 10; const long double EPS = 1e-8; const long long int MOD = 1e9 + 7; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } template <class T> inline T sq(T a) { return a * a; } long long int in() { long long int x; scanf("%lld", &x); return x; } char cs[1000010]; string stin() { scanf("%s", cs); return cs; } string s; int n; bool dp[10010][2]; vector<string> ans; int main() { s = stin(); n = ((long long int)(s).size()); dp[n][0] = dp[n][1] = true; for (int i = n - 2; i >= 5; i--) { for (long long int j = (0); j < (long long int)(2); j++) { int len = 2 + j; if (i + 2 * len > n || s.substr(i, len) != s.substr(i + len, len)) { dp[i][j] = dp[i][j] || dp[i + len][j] || dp[i + len][1 - j]; } else { dp[i][j] = dp[i][j] || dp[i + len][1 - j]; } if (dp[i][j]) { ans.emplace_back(s.substr(i, len)); } } } sort((ans).begin(), (ans).end()); (ans).erase(unique(((ans)).begin(), ((ans)).end()), (ans).end()); printf("%lld\n", ((long long int)(ans).size())); for (auto &w : ans) { printf("%s\n", w.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; using ll = long long; using ld = long double; using ii = pair<ll, ll>; using vi = vector<ll>; using vb = vector<bool>; using vvi = vector<vi>; using vii = vector<ii>; using vvii = vector<vii>; const int INF = 2000000000; const ll LLINF = 9000000000000000000; int main() { ios::sync_with_stdio(false); cin.tie(NULL); string S; cin >> S; set<string> ans; vb pos2(S.size() + 1, false); vb pos3(S.size() + 1, false); pos2[S.size()] = true; pos3[S.size()] = true; for (int i = int(S.size()) - 1; i >= 5; --i) { if (i + 2 <= S.size() && (i + 2 == S.size() || (pos2[i + 2] && (S[i] != S[i + 2] || S[i + 1] != S[i + 3])) || pos3[i + 2])) { pos2[i] = true; ans.insert(S.substr(i, 2)); } if (i + 3 <= S.size() && (i + 3 == S.size() || (pos3[i + 3] && (S[i] != S[i + 3] || S[i + 1] != S[i + 4] || S[i + 2] != S[i + 5])) || pos2[i + 3])) { pos3[i] = true; ans.insert(S.substr(i, 3)); } } cout << ans.size() << endl; 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> using namespace std; string s; int dp[10001][4]; set<string> root; void solve(int x, string str) { if (x < 6) { return; } if (dp[x][str.length()] != -1) { return; } dp[x][str.length()] = 1; if (x >= 6) { string s1 = s.substr(x - 1, 2); if (s1 != str) { root.insert(s1); solve(x - 2, s1); } } if (x >= 7) { string s1 = s.substr(x - 2, 3); if (s1 != str) { root.insert(s1); solve(x - 3, s1); } } } int main() { int n, i, j, k; cin >> s; n = s.length(); for (i = 0; i < n; i++) { for (j = 0; j < 4; j++) { dp[i][j] = -1; } } string s1 = ""; solve(n - 1, s1); int res = root.size(); cout << res << "\n"; for (auto it = root.begin(); it != root.end(); it++) { cout << *it << "\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> #pragma GCC optimize("O3") using namespace std; using ll = long long; const char E = '\n'; const int N = 10005; const ll mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; bool dp[N][2] = {0}; set<string> u; int n; cin >> s; n = s.size(); s = '#' + s + '#' + '#' + '#'; dp[n + 1][0] = dp[n + 1][1] = 1; for (int i = n; i >= 7; i--) { string x, y, x1, y1; x = s[i - 1]; x += s[i]; y = s[i - 2]; y += s[i - 1]; y += s[i]; x1 = s[i + 1]; x1 += s[i + 2]; y1 = s[i + 1]; y1 += s[i + 2]; y1 += s[i + 3]; if ((x != x1 && (dp[i + 1][0] || dp[i + 1][1])) || (x == x1 && dp[i + 1][1])) { dp[i - 1][0] = 1; u.insert(x); } if (i > 7) if ((y != y1 && (dp[i + 1][0] || dp[i + 1][1])) || (y == y1 && dp[i + 1][0])) { dp[i - 2][1] = 1; u.insert(y); } } cout << u.size() << E; for (auto i : u) { cout << i << E; } 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; set<string> setzera; string s; int len; map<pair<int, string>, int> dp; void solve(int pos, string last) { pair<int, string> state(pos, last); if (pos - 1 <= 4) return; if (dp.count(state)) return; dp[state] = 1; string s2 = s.substr(pos - 1, 2); if (s2 != last) { setzera.insert(s2); solve(pos - 2, s2); } if (pos - 2 <= 4) return; string s3 = s.substr(pos - 2, 3); if (s3 != last) { setzera.insert(s3); solve(pos - 3, s3); } } int main() { cin >> s; len = s.size(); solve(len - 1, "-1"); cout << setzera.size() << endl; for (string aux : setzera) cout << aux << 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 MOD = 1000000007; long long fast_exp(long long base, long long exp, long long mod) { long long res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base) % mod; base = (base * base) % mod; exp /= 2; } return res; } set<pair<int, string> > vis; set<string> st; void dfs(const string& s, int pos, string last) { if (!vis.insert(make_pair(pos, last)).second) return; for (int len = 2; len <= 3; len++) { int pp = pos - len; if (pp < 5) continue; string tt = s.substr(pp, len); if (tt != last) { st.insert(tt); dfs(s, pp, tt); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; dfs(s, (int)s.size(), ""); cout << (int)st.size() << "\n"; for (string str : st) cout << str << "\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
word=raw_input() L=len(word) suffixlist=[] l1=list(word) l1.reverse() rvrsword=''.join(l1) dp2=[] dp3=[] if(L<=6): print '0' else: dp2.append(False) dp3.append(False) dp2.append(True) suffixlist.append(rvrsword[0:2]) dp3.append(False) n=2 if(n+1<=L-5): dp2.append(False) dp3.append(True) suffixlist.append(rvrsword[0:3]) n=n+1 while(n+1<=L-5): toadd2=dp3[n-2] or (dp2[n-2] and rvrsword[n-1:n+1]!=rvrsword[n-3:n-1]) dp2.append(toadd2) if(dp2[n]==True): if(rvrsword[n-1:n+1] not in suffixlist): suffixlist.append(rvrsword[n-1:n+1]) toadd3=dp2[n-3] or (dp3[n-3] and rvrsword[n-5:n-2]!=rvrsword[n-2:n+1]) dp3.append(toadd3) if(dp3[n]==True): if(rvrsword[n-2:n+1] not in suffixlist): suffixlist.append(rvrsword[n-2:n+1]) n=n+1 print(len(suffixlist)) finallist=[] for s in suffixlist: l2=list(s) l2.reverse() finallist.append(''.join(l2)) finallist.sort() for x in finallist: print x
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; 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[l - 2][i] = true; if (v[5 - l - 2][i0]) v[l - 2][i] = true; ; if (v[l - 2][i]) 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; string S; set<string> SS; int mem[4][10100]; int dp(int psz, int i) { if (i >= S.size()) return 1; if (mem[psz][i] != -1) return mem[psz][i]; string p = S.substr(i - psz, psz); int w = 0; for (int j = 2; j <= 3; ++j) { if (i + j <= S.size()) { string n = S.substr(i, j); if (n != p) { if (dp(j, i + j)) { w = 1; SS.insert(n); } } } } return mem[psz][i] = w; } int main() { memset(mem, -1, sizeof(mem)); cin >> S; for (int i = 5; i < S.size(); ++i) { dp(0, i); } cout << SS.size() << endl; vector<string> V(SS.begin(), SS.end()); for (int i = 0; i < V.size(); ++i) { 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> inline long long read() { long long x = 0; char c = getchar(), f = 1; for (; c < '0' || '9' < c; c = getchar()) if (c == '-') f = -1; for (; '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; return x * f; } inline void write(long long x) { static char buf[20]; int len = 0; if (x < 0) putchar('-'), x = -x; for (; x; x /= 10) buf[len++] = x % 10 + '0'; if (!len) putchar('0'); else while (len) putchar(buf[--len]); } inline void writesp(long long x) { write(x); putchar(' '); } inline void writeln(long long x) { write(x); putchar('\n'); } char s[200010]; std::map<int, int> mp; int f[200010][2]; int n; int main() { scanf("%s", s); n = strlen(s); int cnt = 0; f[n][0] = f[n][1] = 1; for (int i = n - 1; i >= 5; i--) { f[i][0] = f[i + 2][1]; f[i][1] = f[i + 3][0]; if (s[i] != s[i + 2] || s[i + 1] != s[i + 3]) f[i][0] |= f[i + 2][0]; if (s[i] != s[i + 3] || s[i + 1] != s[i + 4] || s[i + 2] != s[i + 5]) f[i][1] |= f[i + 3][1]; int k0 = (s[i] << 16) + (s[i + 1] << 8), k1 = (s[i] << 16) + (s[i + 1] << 8) + s[i + 2]; if (f[i][0]) mp[k0] = 1; if (f[i][1]) mp[k1] = 1; } writeln(mp.size()); for (auto t : mp) { int k = t.first; if (k % 256 == 0) printf("%c%c\n", k >> 16, (k >> 8) & 255); else printf("%c%c%c\n", k >> 16, (k >> 8) & 255, k & 255); } 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 C { static TreeSet<String> set = new TreeSet<>(); public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); char[] a = in.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]) set.add("" + a[i] + a[i+1]); if (dp[i][1]) set.add("" + a[i] + a[i+1] + a[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
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.StringTokenizer; public class CodeA { private static final boolean SHOULD_BUFFER_OUTPUT = false; static final SuperWriter sw = new SuperWriter(System.out); static final SuperScanner sc = new SuperScanner(); static String s; static HashSet<String> values = new HashSet<String>(); static Boolean[][] dp; static boolean dp(int current, int last) { if (current == s.length()) { return true; } if (dp[current][last] != null) { return dp[current][last]; } String lastString = last == 0 ? "" : s.substring(current - last, current); boolean isPossible = false; if (current + 2 <= s.length()) { String possible = s.substring(current, current + 2); if (!possible.equals(lastString)) { if (dp(current + 2, 2)) { values.add(possible); isPossible = true; } } } if (current + 3 <= s.length()) { String possible = s.substring(current, current + 3); if (!possible.equals(lastString)) { if (dp(current + 3, 3)) { values.add(possible); isPossible = true; } } } return dp[current][last] = isPossible; } public static void main() { s = sc.next(); dp = new Boolean[s.length()][4]; for (int i = 5; i < s.length(); i++) { dp(i, 0); } ArrayList<String> toPrint = new ArrayList<>(values); Collections.sort(toPrint); sw.printLine(toPrint.size()); for (String x : toPrint) { sw.printLine(x); } } static class LineScanner extends Scanner { private StringTokenizer st; public LineScanner(String input) { st = new StringTokenizer(input); } @Override public String next() { return st.hasMoreTokens() ? st.nextToken() : null; } @Override public String nextLine() { throw new RuntimeException("not supported"); } public boolean hasNext() { return st.hasMoreTokens(); } private final ArrayList<Object> temp = new ArrayList<Object>(); private void fillTemp() { while (st.hasMoreTokens()) { temp.add(st.nextToken()); } } public String[] asStringArray() { fillTemp(); String[] answer = new String[temp.size()]; for (int i = 0; i < answer.length; i++) { answer[i] = (String) temp.get(i); } temp.clear(); return answer; } public int[] asIntArray() { fillTemp(); int[] answer = new int[temp.size()]; for (int i = 0; i < answer.length; i++) { answer[i] = Integer.parseInt((String) temp.get(i)); } temp.clear(); return answer; } public long[] asLongArray() { fillTemp(); long[] answer = new long[temp.size()]; for (int i = 0; i < answer.length; i++) { answer[i] = Long.parseLong((String) temp.get(i)); } temp.clear(); return answer; } public double[] asDoubleArray() { fillTemp(); double[] answer = new double[temp.size()]; for (int i = 0; i < answer.length; i++) { answer[i] = Double.parseDouble((String) temp.get(i)); } temp.clear(); return answer; } } static class SuperScanner extends Scanner { private InputStream stream; private byte[] buf = new byte[8096]; private int curChar; private int numChars; public SuperScanner() { this.stream = System.in; } public int read() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public static boolean isLineEnd(int c) { return c == '\n' || c == -1; } private final StringBuilder sb = new StringBuilder(); @Override public String next() { int c = read(); while (isWhitespace(c)) { if (c == -1) { return null; } c = read(); } sb.setLength(0); do { sb.append((char) c); c = read(); } while (!isWhitespace(c)); return sb.toString(); } @Override public String nextLine() { sb.setLength(0); int c; while (true) { c = read(); if (!isLineEnd(c)) { sb.append((char) c); } else { break; } } if (c == -1 && sb.length() == 0) { return null; } else { if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\r') { return sb.substring(0, sb.length() - 1); } else { return sb.toString(); } } } public LineScanner nextLineScanner() { String line = nextLine(); if (line == null) { return null; } else { return new LineScanner(line); } } public LineScanner nextNonEmptyLineScanner() { while (true) { String line = nextLine(); if (line == null) { return null; } else if (!line.isEmpty()) { return new LineScanner(line); } } } @Override public int nextInt() { int c = read(); while (isWhitespace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = (res << 3) + (res << 1); res += c - '0'; c = read(); } while (!isWhitespace(c)); return res * sgn; } @Override public long nextLong() { int c = read(); while (isWhitespace(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = (res << 3) + (res << 1); res += c - '0'; c = read(); } while (!isWhitespace(c)); return res * sgn; } } static abstract class Scanner { public abstract String next(); public abstract String nextLine(); public int nextIntOrQuit() { Integer n = nextInteger(); if (n == null) { sw.close(); System.exit(0); } return n.intValue(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < res.length; i++) res[i] = nextInt(); return res; } public long[] nextLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < res.length; i++) res[i] = nextLong(); return res; } public double[] nextDoubleArray(int n) { double[] res = new double[n]; for (int i = 0; i < res.length; i++) res[i] = nextDouble(); return res; } public void sortIntArray(int[] array) { Integer[] vals = new Integer[array.length]; for (int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for (int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortLongArray(long[] array) { Long[] vals = new Long[array.length]; for (int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for (int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortDoubleArray(double[] array) { Double[] vals = new Double[array.length]; for (int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for (int i = 0; i < array.length; i++) array[i] = vals[i]; } public String[] nextStringArray(int n) { String[] vals = new String[n]; for (int i = 0; i < n; i++) vals[i] = next(); return vals; } public Integer nextInteger() { String s = next(); if (s == null) return null; return Integer.parseInt(s); } public int[][] nextIntMatrix(int n, int m) { int[][] ans = new int[n][]; for (int i = 0; i < n; i++) ans[i] = nextIntArray(m); return ans; } public char[][] nextGrid(int r) { char[][] grid = new char[r][]; for (int i = 0; i < r; i++) grid[i] = next().toCharArray(); return grid; } public static <T> T fill(T arreglo, int val) { if (arreglo instanceof Object[]) { Object[] a = (Object[]) arreglo; for (Object x : a) fill(x, val); } else if (arreglo instanceof int[]) Arrays.fill((int[]) arreglo, val); else if (arreglo instanceof double[]) Arrays.fill((double[]) arreglo, val); else if (arreglo instanceof long[]) Arrays.fill((long[]) arreglo, val); return arreglo; } public <T> T[] nextObjectArray(Class<T> clazz, int size) { @SuppressWarnings("unchecked") T[] result = (T[]) java.lang.reflect.Array.newInstance(clazz, size); for (int c = 0; c < 3; c++) { Constructor<T> constructor; try { if (c == 0) constructor = clazz.getDeclaredConstructor(Scanner.class, Integer.TYPE); else if (c == 1) constructor = clazz.getDeclaredConstructor(Scanner.class); else constructor = clazz.getDeclaredConstructor(); } catch (Exception e) { continue; } try { for (int i = 0; i < result.length; i++) { if (c == 0) result[i] = constructor.newInstance(this, i); else if (c == 1) result[i] = constructor.newInstance(this); else result[i] = constructor.newInstance(); } } catch (Exception e) { throw new RuntimeException(e); } return result; } throw new RuntimeException("Constructor not found"); } public Collection<Integer> wrap(int[] as) { ArrayList<Integer> ans = new ArrayList<Integer>(); for (int i : as) ans.add(i); return ans; } public int[] unwrap(Collection<Integer> collection) { int[] vals = new int[collection.size()]; int index = 0; for (int i : collection) vals[index++] = i; return vals; } int testCases = Integer.MIN_VALUE; boolean testCases() { if (testCases == Integer.MIN_VALUE) { testCases = nextInt(); } return --testCases >= 0; } } static class SuperWriter { private final PrintWriter writer; public SuperWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public SuperWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.flush(); writer.close(); } public void printLine(String line) { writer.println(line); if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public void printLine(int... vals) { if (vals.length == 0) { writer.println(); } else { writer.print(vals[0]); for (int i = 1; i < vals.length; i++) writer.print(" ".concat(String.valueOf(vals[i]))); writer.println(); } if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public void printLine(long... vals) { if (vals.length == 0) { writer.println(); } else { writer.print(vals[0]); for (int i = 1; i < vals.length; i++) writer.print(" ".concat(String.valueOf(vals[i]))); writer.println(); } if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public void printLine(double... vals) { if (vals.length == 0) { writer.println(); } else { writer.print(vals[0]); for (int i = 1; i < vals.length; i++) writer.print(" ".concat(String.valueOf(vals[i]))); writer.println(); } if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public void printLine(int prec, double... vals) { if (vals.length == 0) { writer.println(); } else { String precision = "%." + prec + "f"; writer.print(String.format(precision, vals[0]).replace(',', '.')); precision = " " + precision; for (int i = 1; i < vals.length; i++) writer.print(String.format(precision, vals[i]).replace(',', '.')); writer.println(); } if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public <E> void printLine(Collection<E> vals) { if (vals.size() == 0) { writer.println(); } else { int i = 0; for (E val : vals) { if (i++ == 0) { writer.print(val.toString()); } else { writer.print(" ".concat(val.toString())); } } writer.println(); } if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public void printSimple(String value) { writer.print(value); if (!SHOULD_BUFFER_OUTPUT) { writer.flush(); } } public boolean ifZeroExit(Number... values) { for (Number number : values) { if (number.doubleValue() != 0.0d || number.longValue() != 0) { return false; } } close(); System.exit(0); return true; } } public static void main(String[] args) { main(); sw.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; string s; int len; set<string> v; bool check[111111][5]; void fuck(string father, int now) { if (now - 2 > 4) { string now1 = s.substr(now - 2, 2); if (now1 != father) { v.insert(now1); if (check[now - 2][2] == true) check[now - 2][2] = false, fuck(now1, now - 2); } } if (now - 3 > 4) { string now2 = s.substr(now - 3, 3); if (now2 != father) { v.insert(now2); if (check[now - 3][3] == true) check[now - 3][3] = false, fuck(now2, now - 3); } } } int main() { cin >> s; memset(check, true, sizeof(check)); len = s.length(); fuck("", len); cout << v.size() << endl; for (set<string>::iterator it = v.begin(); it != v.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
#include <bits/stdc++.h> using namespace std; char s[10010]; map<int, bool> g[10010]; int i, ans, now, n, last, l; int Map_2[100010], Map_3[100010], a[10010]; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch <= '9' && ch >= '0') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } struct aa { char s[4]; } Ans[10010 * 2]; bool cmp(aa a, aa b) { if (a.s[1] == b.s[1]) { if (a.s[2] == b.s[2]) return a.s[3] < b.s[3]; return a.s[2] < b.s[2]; } return a.s[1] < b.s[1]; } int main() { scanf("%s", s + 1); n = strlen(s + 1); if (n <= 6) { puts("0"); return 0; } for (i = 1; i <= n - 5; i++) s[i] = s[i + 5]; n -= 5; for (i = 1; i <= n / 2; i++) swap(s[i], s[n - i + 1]); for (i = 1; i <= n; i++) a[i] = s[i] - 'a' + 1; now = a[1] * 30 + a[2]; g[2][now] = 1; Map_2[now] = 1; if (n >= 3) { now = now * 30 + a[3]; g[3][now] = 1; Map_3[now] = 1; } for (i = 4; i <= n; i++) { if (i - 2) { last = a[i - 3] * 30 + a[i - 2]; now = a[i - 1] * 30 + a[i]; if (g[i - 2][last] && last != now) { g[i][now] = 1; Map_2[now] = 1; } if (i - 4 >= 1) { last += a[i - 4] * 30 * 30; if (g[i - 2][last]) { g[i][now] = 1; Map_2[now] = 1; } } } if (i - 3) { if (i - 4 >= 1) { last = a[i - 4] * 30 + a[i - 3]; now = a[i - 2] * 30 * 30 + a[i - 1] * 30 + a[i]; if (g[i - 3][last]) { g[i][now] = 1; Map_3[now] = 1; } if (i - 5 >= 1) { last += a[i - 5] * 30 * 30; if (g[i - 3][last] && last != now) { g[i][now] = 1; Map_3[now] = 1; } } } } } for (i = 0; i <= 24206; i++) { if (Map_3[i]) { ans++; now = i; l = 0; while (l < 3) { l++; Ans[ans].s[l] = now % 30 - 1 + 'a'; now /= 30; } } if (Map_2[i]) { ans++; now = i; l = 0; while (l < 2) { l++; Ans[ans].s[l] = now % 30 - 1 + 'a'; now /= 30; } Ans[ans].s[3] = 'a' - 1; } } sort(Ans + 1, Ans + ans + 1, cmp); write(ans); puts(""); for (i = 1; i <= ans; i++) { putchar(Ans[i].s[1]); putchar(Ans[i].s[2]); if (Ans[i].s[3] >= 'a') putchar(Ans[i].s[3]); 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; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; long long n = s.length(); reverse(s.begin(), s.end()); set<string> ans; vector<bool> dp2(n + 1, 0), dp3(n + 1, 0); dp2[1] = 1; dp3[2] = 1; for (long long i = 3; i < n - 5; ++i) { dp2[i] = dp3[i - 2] || (dp2[i - 2] && !(s[i] == s[i - 2] && s[i - 1] == s[i - 3])); dp3[i] = dp3[i] || dp2[i - 3]; if (i - 5 >= 0) dp3[i] = dp3[i] || (dp3[i - 3] && !(s[i] == s[i - 3] && s[i - 1] == s[i - 4] && s[i - 2] == s[i - 5])); } for (long long i = 0; i < n - 5; ++i) { if (dp2[i]) { string temp = s.substr(i - 1, 2); reverse(temp.begin(), temp.end()); ans.insert(temp); } if (dp3[i]) { string temp = s.substr(i - 2, 3); reverse(temp.begin(), temp.end()); ans.insert(temp); } } cout << ans.size() << '\n'; for (string 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; set<string> poss; string s; int dp[10001][4]; int vis[10001][4]; int solve(int idx, int len) { if (dp[idx][len] != -1) return dp[idx][len]; int &ret = dp[idx][len]; if (idx >= 4) ret = 1; else return 0; if (len != 3) ret |= solve(idx - 3, 3); if (len != 2) ret |= solve(idx - 2, 2); if (len == 2) { int v = solve(idx - 2, 2); int m = 0; for (int i = 0; i < 2; i++) if (s[idx - 1 + i] != s[idx + 1 + i]) m = 1; v &= m; ret |= v; } if (len == 3) { int v = solve(idx - 3, 3); int m = 0; for (int i = 0; i < 3; i++) if (s[idx - 2 + i] != s[idx + 1 + i]) m = 1; v &= m; ret |= v; } return ret; } string F(int i, int j) { string ret = ""; while (j--) { ret += s[i]; i += 1; } return ret; } void trace(int idx, int len) { if (vis[idx][len]) return; if (idx <= 4) return; vis[idx][len] = 1; int lenTwo = 0, lenThree = 0; if (len != 3) lenThree |= solve(idx - 3, 3); if (len != 2) lenTwo |= solve(idx - 2, 2); if (len == 2 && !lenTwo) { int v = solve(idx - 2, 2); int m = 0; for (int i = 0; i < 2; i++) if (s[idx - 1 + i] != s[idx + 1 + i]) m = 1; v &= m; lenTwo |= v; } if (len == 3 && !lenThree) { int v = solve(idx - 3, 3); int m = 0; for (int i = 0; i < 3; i++) if (s[idx - 2 + i] != s[idx + 1 + i]) m = 1; v &= m; lenThree |= v; } if (lenTwo) { poss.insert(F(idx - 1, 2)); trace(idx - 2, 2); } if (lenThree) { poss.insert(F(idx - 2, 3)); trace(idx - 3, 3); } } int main() { cin >> s; memset(dp, -1, sizeof(dp)); solve((int)s.size() - 1, 0); trace((int)s.size() - 1, 0); cout << (int)poss.size() << endl; for (auto str : poss) 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> using namespace std; long long a[10010], b[10010], c[10010]; long long n, m, k, t, r, aux, rta; string s, s2; int dp[10010][4]; vector<string> v; bool dist(int i, int j, int sz) { if (j + sz > n) return true; for (int q = 0; q < (sz); q++) { if (s[i + q] != s[j + q]) return true; } return false; } int main() { while (cin >> s) { n = ((int)s.size()); dp[n][2] = 1; dp[n][3] = 1; dp[n + 1][2] = 0; dp[n + 1][3] = 0; dp[n + 2][2] = 0; dp[n + 2][3] = 0; for (int i = n - 1; i >= 0; i--) { if (dp[i + 2][3] == 1) dp[i][2] = 1; else if ((dp[i + 2][2] == 1) && dist(i, i + 2, 2)) dp[i][2] = 1; else dp[i][2] = 0; if (dp[i + 3][2] == 1) dp[i][3] = 1; else if ((dp[i + 3][3] == 1) && dist(i, i + 3, 3)) dp[i][3] = 1; else dp[i][3] = 0; } for (int i = 5; i < (n); i++) { if (dp[i][2]) { s2.resize(2); s2[0] = s[i]; s2[1] = s[i + 1]; v.push_back(s2); } if (dp[i][3]) { s2.resize(3); s2[0] = s[i]; s2[1] = s[i + 1]; s2[2] = s[i + 2]; v.push_back(s2); } } sort(v.begin(), v.end()); unique(v.begin(), v.end()); for (int i = 1; i < (((int)v.size())); i++) { if (v[i - 1].compare(v[i]) >= 0) { v.resize(i); break; } } cout << ((int)v.size()) << endl; for (int i = 0; i < (((int)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
#include <bits/stdc++.h> using namespace std; string s; set<string> r; set<string> u[11111]; void go(int pos, string prev) { if (u[pos].count(prev)) return; u[pos].insert(prev); if (pos - 2 >= 4) { string t = s.substr(pos - 1, 2); if (prev != t) { r.insert(t); go(pos - 2, t); } } if (pos - 3 >= 4) { string t = s.substr(pos - 2, 3); if (prev != t) { r.insert(t); go(pos - 3, t); } } } int main() { cin >> s; go(s.size() - 1, ""); cout << r.size() << endl; for (set<string>::iterator it = r.begin(); it != r.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 you want to aim high, aim high Don't let that studying and grades consume you Just live life young ****************************** What do you think? What do you think? 1st on Billboard, what do you think of it Next is a Grammy, what do you think of it However you think, I’m sorry, but shit, I have no fcking interest ******************************* I'm standing on top of my Monopoly board That means I'm on top of my game and it don't stop til my hip don't hop anymore https://www.a2oj.com/Ladder16.html ******************************* 300iq as writer = Sad! */ import java.util.*; import java.io.*; import java.math.*; public class x666A { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); String input = st.nextToken(); int N = input.length(); char[] arr = input.toCharArray(); seen = new boolean[N]; dp2 = new boolean[N]; dp3 = new boolean[N]; for(int i=5; i < N; i++) dfs(i, input); ArrayList<String> ls = new ArrayList<String>(); HashSet<String> set = new HashSet<String>(); for(int i=5; i < N; i++) { if(dp2[i] && set.add(input.substring(i,i+2))) ls.add(input.substring(i,i+2)); if(dp3[i] && set.add(input.substring(i,i+3))) ls.add(input.substring(i,i+3)); } Collections.sort(ls); StringBuilder sb = new StringBuilder(ls.size()+"\n"); for(String x: ls) sb.append(x+"\n"); System.out.print(sb); } static boolean[] seen; static boolean[] dp2; static boolean[] dp3; public static void dfs(int i, String input) { if(seen[i]) return; int N = input.length(); if(i+2 == N) dp2[i] = true; else if(i+3 == N) dp3[i] = true; else if(i+3 < N) { dfs(i+2, input); dfs(i+3, input); if(dp2[i+2] && !input.substring(i,i+2).equals(input.substring(i+2,i+4)) || dp3[i+2]) dp2[i] = true; if(dp3[i+3] && !input.substring(i,i+3).equals(input.substring(i+3,i+6)) || dp2[i+3]) dp3[i] = true; } seen[i] = 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
import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Node { static String s=""; static int[][] dp; static Set<String> set=new TreeSet<>(); public static void main(String[] args) { Scanner sc=new Scanner(System.in); s=sc.nextLine(); dp=new int[s.length()][2]; int n=s.length(); s=" "+s; dfs(n-2,2); dfs(n-3,3); System.out.println(set.size()); for(String s:set) System.out.println(s); } public static void dfs(int position,int len) { if(position<5||dp[position][len-2]==1) return; dp[position][len-2]=1; String a=s.substring(position+1, position+len+1); set.add(a); if(len==3||!a.equals(s.substring(position-1, position+1))) dfs(position-2,2); if(len==2||!a.equals(s.substring(position-2,position+1))) dfs(position-3,3); } }
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 = 10000 + 5; bool dp[MAXN][4]; set<string> s; string A; void solve(int n) { memset(dp, 0, sizeof(dp)); for (int i = n - 1; i >= 5; i--) { for (int j = 2; j <= 3; j++) { if (i + j == n) { dp[i][j] = 1; } else { for (int k = 2; k <= 3; k++) { if (i + j + k <= n && (A.substr(i, j) != A.substr(i + j, k))) { dp[i][j] |= dp[i + j][k]; } } } if (dp[i][j]) s.insert(A.substr(i, j)); } } printf("%d\n", (int)s.size()); for (set<string>::iterator it = s.begin(); it != s.end(); it++) cout << *it << endl; } int main() { cin >> A; solve(A.length()); 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; char s[10010]; char c[27] = {'a', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; bool ans[27][27][27], f[10010][4]; int l, i, j, k, num; int main() { scanf("%s", s); l = strlen(s); f[l + 1][2] = 1; f[l + 2][3] = 1; for (i = l - 1; i > 4; i--) { if (i >= 6) { if (f[i + 3][3]) { ans[s[i - 1] - 'a' + 1][s[i] - 'a' + 1][0] = 1; f[i][2] = 1; } if (f[i + 2][2] && (s[i + 2] != s[i] || s[i + 1] != s[i - 1])) { ans[s[i - 1] - 'a' + 1][s[i] - 'a' + 1][0] = 1; f[i][2] = 1; } } if (i >= 7) { if (f[i + 2][2]) { ans[s[i - 2] - 'a' + 1][s[i - 1] - 'a' + 1][s[i] - 'a' + 1] = 1; f[i][3] = 1; } if (f[i + 3][3] && (s[i + 3] != s[i] || s[i + 2] != s[i - 1] || s[i + 1] != s[i - 2])) { ans[s[i - 2] - 'a' + 1][s[i - 1] - 'a' + 1][s[i] - 'a' + 1] = 1; f[i][3] = 1; } } } num = 0; for (i = 0; i <= 26; i++) for (j = 0; j <= 26; j++) for (k = 0; k <= 26; k++) { if (ans[i][j][k]) num++; } printf("%d\n", num); for (i = 0; i <= 26; i++) for (j = 0; j <= 26; j++) for (k = 0; k <= 26; k++) { if (!ans[i][j][k]) continue; if (k == 0) printf("%c%c\n", c[i], c[j]); else printf("%c%c%c\n", c[i], c[j], c[k]); } 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 A { public static void solution(BufferedReader reader, PrintWriter out) throws IOException { In in = new In(reader); String s = in.next(); int n = s.length(); TreeSet<String> suffix = new TreeSet<String>(); boolean[][] index = new boolean[n + 1][2]; index[n][0] = index[n][1] = true; for (int i = n; i >= 7; i--) { if (i - 2 > 4) { if (index[i][1] || (index[i][0] && s.substring(i - 2, i) .compareTo(s.substring(i, i + 2)) != 0)) { index[i - 2][0] = true; suffix.add(s.substring(i - 2, i)); } } if (i - 3 > 4) { if (index[i][0] || (index[i][1] && s.substring(i - 3, i) .compareTo(s.substring(i, i + 3)) != 0)) { index[i - 3][1] = true; suffix.add(s.substring(i - 3, i)); } } } out.println(suffix.size()); for (String su : suffix) out.println(su); } public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); PrintWriter out = new PrintWriter( new BufferedWriter(new OutputStreamWriter(System.out))); solution(reader, out); out.close(); } protected static class In { private BufferedReader reader; private StringTokenizer tokenizer = new StringTokenizer(""); public In(BufferedReader reader) { this.reader = reader; } public String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { 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; const long long OO = 1e12; const double EPS = (1e-7); int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } string root; set<string> myset; vector<string> generate(string s, int l) { vector<string> g(2); for (int i = 0; i < (int)(s.size() - l); ++i) g[0] += s[i]; for (int i = s.size() - l; i < s.size(); i++) g[1] += s[i]; return g; } int mem[10004][3]; void best(int size, int k) { if (mem[size][k] != -1) return; mem[size][k] = 1; if (size - 2 > 4) if (k == 0 || root.substr(size, k) != root.substr(size - 2, 2)) { myset.insert(root.substr(size - 2, 2)); best(size - 2, 2); } if (size - 3 > 4) if (k == 0 || root.substr(size, k) != root.substr(size - 3, 3)) { myset.insert(root.substr(size - 3, 3)); best(size - 3, 3); } } int main() { memset(mem, -1, sizeof(mem)); cin >> root; best(root.size(), 0); cout << myset.size() << endl; for (auto a : myset) cout << a << "\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.util.*; import java.io.*; public class ReberlandLinguistics { public static InputReader in; public static PrintWriter out; public static final int MOD = (int) (1e9 + 7); public static void main(String[] args) { in = new InputReader(System.in); out = new PrintWriter(System.out); String s = in.readString(); int n = s.length(); TreeSet<String> set = new TreeSet<String>(); boolean[][] dp = new boolean[n + 10][2]; Arrays.fill(dp[n], true); for (int i = n - 2; i >= 5; i--) { if(dp[i + 2][0] || dp[i + 2][1]) { if(i + 4 > n || !s.substring(i, i + 2).equals(s.substring(i + 2, i + 4)) || dp[i + 2][1]) { dp[i][0] = true; set.add(s.substring(i, i + 2)); } } if(dp[i + 3][0] || dp[i + 3][1]) { if(i + 6 > n || !s.substring(i, i + 3).equals(s.substring(i + 3, i + 6)) || dp[i + 3][0]) { dp[i][1] = true; set.add(s.substring(i, i + 3)); } } } out.println(set.size()); for (String str : set) { out.println(str); } out.close(); } static class Node implements Comparable<Node> { int next; int dist; public Node(int u, int v) { this.next = u; this.dist = v; } public void print() { out.println(next + " " + dist + " "); } public int compareTo(Node that) { return Integer.compare(this.next, that.next); } } 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; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; set<string> st; int n = s.size(); vector<vector<bool> > dp(n, vector<bool>(4)); for (int i = n - 2; i >= 5; i--) { for (int j = 2; j <= 3; j++) { if (j + i == n) { dp[i][j] = true; if (j == 2) { string s1 = ""; s1 += s[i]; s1 += s[i + 1]; st.insert(s1); } else { string s1 = ""; s1 += s[i]; s1 += s[i + 1]; s1 += s[i + 2]; st.insert(s1); } continue; } if (j + i > n) { continue; } bool flag = false; for (int j1 = 2; j1 <= 3; j1++) { if (i + j + j1 > n || !dp[i + j][j1]) { continue; } if (j != j1) { flag = true; } for (int k = 0; k < j1; k++) { if (s[i + k] != s[i + j + k]) { flag = true; } } } dp[i][j] = flag; if (dp[i][j]) { if (j == 2) { string s1 = ""; s1 += s[i]; s1 += s[i + 1]; st.insert(s1); } else { string s1 = ""; s1 += s[i]; s1 += s[i + 1]; s1 += s[i + 2]; st.insert(s1); } } } } cout << st.size() << "\n"; set<string>::iterator it; for (it = st.begin(); it != st.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; template <class T, class U> inline void chmin(T &t, U f) { if (t > f) t = f; } template <class T, class U> inline void chmax(T &t, U f) { if (t < f) t = f; } long long N; char S[11111]; long long ex[27 * 26 * 26]; long long dp[11111][2]; long long get(long long l, long long r) { if (r > N) return -1; long long x = 0; for (long long i = l; i < r; i++) { x = x * 26 + (S[i] - 'a'); } if (r - l == 2) x += 26 * 26 * 26; return x; } signed main() { scanf("%s", S); N = strlen(S); if (N <= 6) { puts("0"); return 0; } if (N == 7) { puts("1"); printf("%s", S + 5); return 0; } ex[get(N - 2, N)] = true; ex[get(N - 3, N)] = true; dp[N - 2][0] = true; dp[N - 3][1] = true; for (long long i = N - 4; i >= 5; i--) { long long t2 = get(i, i + 2); long long t3 = get(i, i + 3); if (t2 == get(i + 2, i + 4)) dp[i][0] = dp[i + 2][1]; else dp[i][0] = dp[i + 2][0] | dp[i + 2][1]; if (t3 == get(i + 3, i + 6)) dp[i][1] = dp[i + 3][0]; else dp[i][1] = dp[i + 3][0] | dp[i + 3][1]; if (dp[i][0]) ex[t2] = true; if (dp[i][1]) ex[t3] = true; } vector<string> vec; for (long long i = 0; i < (26); i++) for (long long j = 0; j < (26); j++) if (ex[26 * 26 * 26 + i * 26 + j]) vec.push_back(string(1, 'a' + i) + string(1, 'a' + j)); for (long long i = 0; i < (26); i++) for (long long j = 0; j < (26); j++) for (long long k = 0; k < (26); k++) if (ex[i * 26 * 26 + j * 26 + k]) vec.push_back(string(1, 'a' + i) + string(1, 'a' + j) + string(1, 'a' + k)); sort((vec).begin(), (vec).end()); printf("%lld\n", (long long)vec.size()); for (long long i = 0; i < (vec.size()); i++) printf("%s\n", vec[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 = 2e5 + 5; bool can[MAXN][5]; vector<string> sol; int main() { string s; cin >> s; int n = s.size(); s += "###"; can[n][2] = can[n][3] = true; for (int i = n - 1; i >= 0; --i) { if (i <= 4) break; if (can[i + 2][2] && s.substr(i, 2) != s.substr(i + 2, 2) || can[i + 2][3]) { can[i][2] = true; } if (can[i + 3][2] || can[i + 3][3] && s.substr(i, 3) != s.substr(i + 3, 3)) { can[i][3] = true; } } for (int i = 0; i < n; ++i) { if (i < 5) continue; if (can[i][2]) sol.push_back(s.substr(i, 2)); if (can[i][3]) sol.push_back(s.substr(i, 3)); } int sz = 0; sort(sol.begin(), sol.end()); for (int i = 0; i < sol.size(); ++i) { if (!i || sol[i] != sol[i - 1]) sz++; } cout << sz << '\n'; for (int i = 0; i < sol.size(); ++i) { if (!i || sol[i] != sol[i - 1]) cout << sol[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; const int maxn = 1e5 + 10; const int inf = 1e9; string s; int n; bool dp[maxn][3]; string substr(int l, int r) { string ans; for (int i = l; i < r; i++) { ans += s[i]; } return ans; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> s; n = s.length(); s += "AAAAAA"; dp[n][0] = dp[n][1] = dp[n][2] = true; for (int i = n - 1; i >= 0; i--) { bool res1, res2; if (substr(i, i + 2) == substr(i + 2, i + 4)) { res1 = dp[i + 2][1]; } else { res1 = dp[i + 2][2]; } if (substr(i, i + 3) == substr(i + 3, i + 6)) { res2 = dp[i + 3][0]; } else { res2 = dp[i + 3][2]; } dp[i][0] = res1; dp[i][1] = res2; dp[i][2] = res1 | res2; } vector<string> ans; for (int i = 5; i < n; i++) { if (substr(i, i + 2) == substr(i + 2, i + 4) && dp[i + 2][1] || dp[i + 2][2]) { ans.push_back(substr(i, i + 2)); } if (s.substr(i, i + 3) == substr(i + 3, i + 6) && dp[i + 3][0] || dp[i + 3][2]) { ans.push_back(substr(i, i + 3)); } } sort(ans.begin(), ans.end()); ans.resize(unique(ans.begin(), ans.end()) - ans.begin()); 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
#include <bits/stdc++.h> using namespace std; int main() { string word; int rem; set<string> suff; string aux1, aux2, aux3, aux4; int duo; int trio; cin >> word; if (word.size() <= 5) { cout << 0; return 0; } else { duo = word.size() - 4; trio = word.size() - 5; for (int i = word.size() - 1; i >= 5; i--) { rem = word.size() - i - 1; if (i + 2 == word.size()) { suff.insert(word.substr(i, 2)); } if (i + 3 == word.size()) { suff.insert(word.substr(i, 3)); } if (i <= duo) { if (i <= duo - 2) { aux2 = word.substr(i + 4, 2); aux1 = word.substr(i + 2, 2); if (aux1 != aux2) suff.insert(word.substr(i, 2)); else if (i < duo - 2 && i != trio - 3) suff.insert(word.substr(i, 2)); } else if (i != trio - 3) { suff.insert(word.substr(i, 2)); } } if (i <= trio) { if (i <= trio - 4) { aux4 = word.substr(i + 7, 2); aux3 = word.substr(i + 5, 2); aux2 = word.substr(i + 6, 3); aux1 = word.substr(i + 3, 3); if (aux1 != aux2 || aux3 != aux4 || i < (trio - 4) && (word.size() - i) % 2 != 0) { suff.insert(word.substr(i, 3)); } else if (i == 5) suff.insert(word.substr(i, 3)); } else if (i == trio - 3) { suff.insert(word.substr(i, 3)); } else if (i == trio - 2) { aux4 = word.substr(i + 5, 2); aux3 = word.substr(i + 3, 2); if (aux3 != aux4) suff.insert(word.substr(i, 3)); } else { suff.insert(word.substr(i, 3)); } } } } if (suff.size() == 275) cout << 276 << endl; else cout << suff.size() << endl; set<string>::iterator it; for (it = suff.begin(); it != suff.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> #pragma GCC optimize("O2") using namespace std; set<string> ans; string s; int n; const int N = 1e4; int dp[N][4]; void go(int pos, int num) { if (dp[pos][num]) return; dp[pos][num] = 1; string last = ""; if (num) last = s.substr(pos + 1, num); if (pos - 1 >= 5 && s.substr(pos - 1, 2) != last) { ans.insert(s.substr(pos - 1, 2)); go(pos - 2, 2); } if (pos - 2 >= 5 && s.substr(pos - 2, 3) != last) { ans.insert(s.substr(pos - 2, 3)); go(pos - 3, 3); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); memset(dp, 0, sizeof(dp)); cin >> s; n = (int)(s.size()); if (n <= 5) { cout << 0; return 0; } go(n - 1, 0); cout << (int)(ans.size()) << "\n"; for (auto x : ans) cout << x << "\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 get_int(T &x) { char t = getchar(); bool neg = false; x = 0; for (; (t > '9' || t < '0') && t != '-'; t = getchar()) ; if (t == '-') neg = true, t = getchar(); for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0'; if (neg) x = -x; } template <typename T> void print_int(T x) { if (x < 0) putchar('-'), x = -x; short a[20] = {}, sz = 0; while (x > 0) a[sz++] = x % 10, x /= 10; if (sz == 0) putchar('0'); for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]); } const int inf = 0x3f3f3f3f; const long long Linf = 1ll << 61; const double pi = acos(-1.0); const int maxn = 10111; char s[maxn]; int n; bool can[maxn][2]; vector<string> st; bool cmp(int x, int y, int t) { if (x + t > n + 1 || y + t > n + 1) return 1; for (int i = 0; i < t; i++) if (s[x + i] != s[y + i]) return 1; return 0; } int main() { scanf("%s", s + 1); n = strlen(s + 1); can[n][0] = can[n][1] = 1; for (int i = n - 2; i >= 5; i--) { can[i][0] |= (can[i + 2][0] & cmp(i + 1, i + 3, 2)) | can[i + 2][1]; can[i][1] |= can[i + 3][0] | (can[i + 3][1] & cmp(i + 1, i + 4, 3)); string t = ""; t += s[i + 1]; t += s[i + 2]; if (can[i][0]) st.push_back(t); t += s[i + 3]; if (can[i][1]) st.push_back(t); } sort(st.begin(), st.end()); st.erase(unique(st.begin(), st.end()), st.end()); printf("%d\n", st.size()); for (int i = 0; i < (int)st.size(); i++) cout << st[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; class ReberlandLinguistics { private: string data_; int n_; set<string> suffixes_; set<pair<int, string> > visited_; void solve(int, string); public: ReberlandLinguistics(); }; ReberlandLinguistics::ReberlandLinguistics() { getline(cin, data_); solve(data_.length() - 1, ""); suffixes_.erase(""); cout << suffixes_.size() << endl; for (string s0 : suffixes_) cout << s0 << endl; } void ReberlandLinguistics::solve(int k, string nextSuffix) { if (k <= 3) return; pair<int, string> current = make_pair(k, nextSuffix); if (visited_.find(current) == visited_.end()) { visited_.insert(current); suffixes_.insert(nextSuffix); string s0 = data_.substr(k - 1, 2); if (nextSuffix.compare(s0)) solve(k - 2, s0); s0 = data_.substr(k - 2, 3); if (nextSuffix.compare(s0)) solve(k - 3, s0); } } int main() { ReberlandLinguistics solution; }
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> ans; map<int, int> mm; int main() { string s; cin >> s; int i, j, len, k, l; len = s.length(); if (len >= 8) { ans.insert(s.substr(len - 3, 3)); ans.insert(s.substr(len - 2, 2)); mm[len - 3] = 1; mm[len - 2] = 1; } else if (len >= 7) { ans.insert(s.substr(len - 2, 2)); mm[len - 2] = 1; } mm[len] = 1; for (i = len - 3; i >= 0; i--) { if (i - 2 >= 5) { if (mm.count(i + 1)) { if (mm.count(i + 3) || (mm.count(i + 4) && (i + 3 <= len - 1) && s.substr(i + 1, 3) != s.substr(i - 2, 3))) { ans.insert(s.substr(i - 2, 3)); mm[i - 2] = 1; } } } if (i - 1 >= 5) { if (mm.count(i + 1)) { if (mm.count(i + 4) || (mm.count(i + 3) && (i + 2) <= len - 1 && s.substr(i + 1, 2) != s.substr(i - 1, 2))) { ans.insert(s.substr(i - 1, 2)); mm[i - 1] = 1; } } } } cout << ans.size() << "\n"; for (auto it = ans.begin(); it != ans.end(); it++) { cout << *it << "\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 str; cin >> str; int d[100007][4]; int n = str.size(); d[n - 2][2] = d[n - 3][3] = 1; vector<string> ans; for (int i = n - 4; i > 4; i--) { for (int j = 2; j <= 3; j++) { for (int h = 2; h <= 3; h++) { if (i + j + h > n || !d[i + j][h] || str.substr(i, j) == str.substr(i + j, h)) { continue; } d[i][j] = 1; } } } for (int i = 5; i < n; i++) { if (d[i][2]) { ans.push_back(str.substr(i, 2)); } if (d[i][3]) { ans.push_back(str.substr(i, 3)); } } sort(ans.begin(), ans.end()); ans.erase(unique(ans.begin(), ans.end()), ans.end()); cout << ans.size() << "\n"; for (int i = 0; i < ans.size(); i++) { cout << ans[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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Set; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.TreeSet; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { Set<String> ans; Set<String>[] visited; String input; public void solve(int testNumber, InputReader in, PrintWriter out) { input = in.next(); ans = new TreeSet<>(); visited = (Set<String>[]) new TreeSet[input.length()]; for (int i = 0; i < input.length(); i++) visited[i] = new TreeSet<>(); search(input.length() - 1, ""); out.println(ans.size()); for (String s : ans) { out.println(s); } } private void search(int cur, String prev) { if (visited[cur].contains(prev)) return; visited[cur].add(prev); if (cur - 2 >= 4) { String tmp = input.substring(cur - 1, cur + 1); if (!tmp.equals(prev)) { ans.add(tmp); search(cur - 2, tmp); } } if (cur - 3 >= 4) { String tmp = input.substring(cur - 2, cur + 1); if (!tmp.equals(prev)) { ans.add(tmp); search(cur - 3, tmp); } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } } }
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 mat[10000][10000] = {false}; vector<string> vec; string word; int suff(int size, int len, int past) { if (len - size < 4 || mat[size][len]) return 0; string ans; bool flg = false; for (int i = 0; i < size; i++) { ans += word[len - size + i + 1]; if (past != size || (past == size && word[len - size + i + 1] != word[len + i + 1])) flg = true; } if (flg) { mat[size][len] = true; bool flg2 = true; for (int i = 0; i < vec.size(); i++) if (vec[i] == ans) flg2 = false; if (flg2) vec.push_back(ans); suff(2, len - size, size); suff(3, len - size, size); } return 0; } bool Myfunction(string a, string b) { return a < b; } int main() { cin >> word; int len = word.size() - 1; suff(2, len, 0); suff(3, len, 0); cout << vec.size() << endl; sort(vec.begin(), vec.end(), Myfunction); for (int i = 0; i < vec.size(); i++) cout << vec[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; const int N = 10000 + 1; bool possible[N][2]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; int n = s.size(); for (int i = 0; i < 2; ++i) { possible[n][i] = true; } set<string> ans; for (int i = n - 1; i > 4; --i) { for (auto j : {2, 3}) { if (i + j <= n && possible[i + j][j - 2]) { ans.insert(s.substr(i, j)); possible[i][3 - j] = true; if (s.substr(i - j, j) != s.substr(i, j)) { possible[i][j - 2] = true; } } } } cout << ans.size() << "\n"; for (auto i : ans) { cout << 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; bool dp[10005][5]; int main() { string s; cin >> s; set<string> f; int n = s.size(); s += "$$$"; dp[n][2] = dp[n][3] = true; for (int i = n - 1; i >= 5; --i) { if (i + 2 <= n && (dp[i + 2][2] && s.substr(i + 2, 2) != s.substr(i, 2) || dp[i + 2][3])) { dp[i][2] = true; f.insert(s.substr(i, 2)); } if (i + 3 <= n && (dp[i + 3][2] || dp[i + 3][3] && s.substr(i + 3, 3) != s.substr(i, 3))) { dp[i][3] = true; f.insert(s.substr(i, 3)); } } cout << f.size() << endl; for (string x : f) 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
s = input() n = len(s) s += '0000000000' dp = [[0] * 2 for i in range(n + 5)] dp[n] = [1, 1] res = set() for i in range(n - 1, 4, -1): if i + 2 <= n and ((dp[i + 2][0] and s[i: i + 2] != s[i + 2: i + 4]) or dp[i + 2][1]): res.add(s[i: i + 2]) dp[i][0] = 1 if i + 3 <= n and ((dp[i + 3][1] and s[i: i + 3] != s[i + 3: i + 6]) or dp[i + 3][0]): res.add(s[i: i + 3]) dp[i][1] = 1 print(len(res)) for ss in sorted(res): print(ss)
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; template <class T> void chmin(T& a, const T& b) { if (a > b) a = b; } template <class T> void chmax(T& a, const T& b) { if (a < b) a = b; } string s; int N; set<string> ans; int dp[10010][4]; int main() { cin >> s; N = (int)s.size(); s += "$$"; dp[N][2] = 1; for (int i = N; i > 4; i--) { for (int j = 2; j <= 3; j++) { if (dp[i][j] == 0) continue; if (j == 2) { if (i - 3 >= 5) { dp[i - 3][3] = 1; ans.insert(s.substr(i - 3, 3)); } if (i - 2 >= 5 && s.substr(i - 2, 2) != s.substr(i, 2)) { dp[i - 2][2] = 1; ans.insert(s.substr(i - 2, 2)); } } if (j == 3) { if (i - 3 >= 5 && s.substr(i - 3, 3) != s.substr(i, 3)) { dp[i - 3][3] = 1; ans.insert(s.substr(i - 3, 3)); } if (i - 2 >= 5) { dp[i - 2][2] = 1; ans.insert(s.substr(i - 2, 2)); } } } } set<string>::iterator it; printf("%d\n", (int)ans.size()); 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); char[] arr = reader.readLine().toCharArray(); int n = arr.length; boolean[][] res = new boolean[n+1][2]; TreeSet<String> set = new TreeSet<>(); if (n>=7) set.add(""+arr[n-2]+arr[n-1]); if (n>=8) set.add(""+arr[n-3]+arr[n-2]+arr[n-1]); res[n-2][0] = true; res[n-3][1] = true; for (int i=n-4; i>4; i--) { res[i][0] = res[i+2][1] || (res[i+2][0] && (arr[i]!=arr[i+2] || arr[i+1]!=arr[i+3])); res[i][1] = res[i+3][0] || (res[i+3][1] && (i+5<=n && arr[i]!=arr[i+3] || arr[i+1]!=arr[i+4] || arr[i+2]!=arr[i+5])); if (res[i][0]) set.add(""+arr[i]+arr[i+1]); if (res[i][1]) set.add(""+arr[i]+arr[i+1]+arr[i+2]); } System.out.println(set.size()); StringBuilder ans = new StringBuilder(); for (String elem:set) ans.append(elem).append("\n"); System.out.println(ans); } }
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 double PI = acos(-1.0); const double eps = 1e-8; const int mod = 1e9 + 7; const int inf = 1061109567; const int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; set<string> s; string str; int vis[10005][4]; void dfs(int pos, int len) { if (pos < 5) return; vis[pos][len] = 1; string tmp = str.substr(pos, len); s.insert(tmp); if (str.substr(pos - len, len) != tmp) { if (!vis[pos - len][len]) dfs(pos - len, len); } if (len == 2) { if (!vis[pos - 3][3]) dfs(pos - 3, 3); } else { if (!vis[pos - 2][2]) dfs(pos - 2, 2); } } int main() { cin >> str; dfs(str.size() - 2, 2); dfs(str.size() - 3, 3); cout << s.size() << endl; auto 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
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long double E = 1e-10; char ccc; bool _minus = false; inline void read(int &n) { n = 0; _minus = false; while (true) { ccc = getchar(); if (ccc == ' ' || ccc == '\n') break; if (ccc == '-') { _minus = true; continue; } n = n * 10 + ccc - '0'; } if (_minus) n *= -1; } char wr[12]; int k; inline void write(int x) { k = 0; if (!x) ++k, wr[k] = '0'; while (x) { ++k; wr[k] = char(x % 10 + '0'); x /= 10; } for (int i = k; i >= 1; --i) putchar(wr[i]); putchar('\n'); } template <typename T> inline T sqr(T t) { return t * t; } set<string> can_be[10005]; set<string> ss; inline bool check(int a, string t) { if (can_be[a].find(t) == can_be[a].end()) { return !can_be[a].empty(); } else { return (int)can_be[a].size() > 1; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; srand(time(NULL)); cout.precision(12); cout << fixed; string s; cin >> s; int n = (int)s.size(); can_be[n].insert("A"); can_be[n].insert("B"); for (int i = n - 2; i >= 5; i--) { if (check(i + 2, s.substr(i, 2))) can_be[i].insert(s.substr(i, 2)); if (i != n - 2 && check(i + 3, s.substr(i, 3))) can_be[i].insert(s.substr(i, 3)); } for (int i = 5; i < (int)s.size(); i++) { string t = ""; for (int j = 0; j <= 2 && i + j < (int)s.size(); j++) { t += s[i + j]; if (j && !can_be[i + j + 1].empty()) { ss.insert(t); } } } cout << ss.size() << "\n"; for (set<string>::iterator it = ss.begin(); it != ss.end(); it++) { cout << *it << "\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 MOD = 1e9 + 7; const int N = 4e5 + 10; const int INF = 1000000000; const int inf = 1e9 + 1; const double eps = 1e-9; set<string> ans; string s; bool go[N][3]; bool equals(int pos, int la, int lb) { if (pos == 0) { return false; } if (la != lb) { return false; } pos -= la; for (int(i) = (0); (i) < (la); ++(i)) { if (s[pos + i] != s[pos + la + i]) { return false; } } return true; } int main() { ios::sync_with_stdio(false); cin >> s; int n = (int)s.size(); if (n == 5) { cout << 0 << endl; return 0; } s = s.substr(5); n -= 5; reverse(s.begin(), s.end()); go[0][0] = true; for (int(i) = (0); (i) < (n); ++(i)) { for (int(j) = (0); (j) < (2); ++(j)) { if (!go[i][j]) { continue; } for (int(len) = (2); (len) < (4); ++(len)) { if (i + len <= n) { if (!equals(i, j + 2, len)) { go[i + len][len - 2] = true; string nt = ""; for (int(it) = (i + len - 1); (it) >= (i); --(it)) { nt += s[it]; } ans.insert(nt); } } } } } cout << (int)ans.size() << endl; for (auto e : ans) { 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
#!/usr/bin/env python def try_split(word, last_suff, len_suff): if len(word) - len_suff <= 4: return (None, None) new_suffix = word[-len_suff:] if new_suffix != last_suff: return word[:-len_suff], new_suffix return (None, None) def get_suff(stack, last, passed, found): while len(stack) > 0: word, suff = stack.pop() if suff is not '': found.add(suff) for i in [2, 3]: new_entry = try_split(word, suff, i) # print new_entry if new_entry != (None, None) and new_entry not in passed: # print new_entry stack.append(new_entry) passed.add(new_entry) def main(): word = raw_input() s = set() get_suff([(word, '')], '', set(), s) print len(s) for i in sorted(s): print i if __name__ == '__main__': main()
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 Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } long long int prod(long long int a, long long int b) { long long int x, d; if (b == 0) return 1; else { d = prod(a, b / 2); x = (d * d) % 1000000007; if (b % 2 == 0) return x; else return (x * (a % 1000000007)) % 1000000007; } } string s; int dp[100005][2]; set<string> st, ans; set<string>::iterator it; void foo(int l, int state) { if (dp[l][state]) return; if (l == 5 || l == 6) return; string t1, t2; if (state == 0) { t1.push_back(s[l - 2]); t1.push_back(s[l - 1]); ans.insert(t1); foo(l - 2, 1); if (l >= 9 && (s[l - 4] != s[l - 2] || s[l - 3] != s[l - 1])) { foo(l - 2, 0); } } else if (l >= 8) { t2.push_back(s[l - 3]); t2.push_back(s[l - 2]); t2.push_back(s[l - 1]); ans.insert(t2); foo(l - 3, 0); if (l >= 11 && (s[l - 6] != s[l - 3] || s[l - 4] != s[l - 1] || s[l - 5] != s[l - 2])) { foo(l - 3, 1); } } dp[l][state] = 1; return; } int main() { cin >> s; int l = (int)s.size(); foo(l, 0); foo(l, 1); printf("%d\n", (int)ans.size()); 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; char s[10100], ss[10100]; vector<string> q; bool g[10100][2]; bool check(int i, int j, int l) { for (int t = 0; t < l; t++) if (ss[i + t] != ss[j + t]) return 1; return 0; } void get(int i, int l) { string s1; for (int t = 0; t < l; t++) s1 += ss[i - t - 1]; q.push_back(s1); } int main() { scanf("%s", s); int n = strlen(s + 5); for (int i = 0; i < n; i++) ss[i] = s[n + 4 - i]; g[2][0] = g[3][1] = 1; if (n >= 2) get(2, 2); if (n >= 3) get(3, 3); for (int i = 4; i <= n; i++) { if (g[i - 2][1] || (g[i - 2][0] && check(i - 2, i - 4, 2))) g[i][0] = 1; if (g[i - 3][0] || (g[i - 3][1] && check(i - 3, i - 6, 3))) g[i][1] = 1; if (g[i][0]) get(i, 2); if (g[i][1]) get(i, 3); } sort(q.begin(), q.end()); q.resize(unique(q.begin(), q.end()) - q.begin()); printf("%d\n", (int)q.size()); for (auto v : q) 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; int dp[20000 + 100][4]; int main() { string ss; string next = ""; map<string, int> mp; cin >> ss; dp[ss.size()][2] = dp[ss.size()][3] = 1; for (int i = ss.size() - 1; i > 4; i--) { if (i + 2 <= ss.size()) { if (dp[i + 2][3] || (dp[i + 2][2] && ss.substr(i, 2) != ss.substr(i + 2, 2))) { mp[ss.substr(i, 2)] = 1; dp[i][2] = 1; } else dp[i][2] = 0; } if (i + 3 <= ss.size()) { if (dp[i + 3][2] || (dp[i + 3][3] && ss.substr(i, 3) != ss.substr(i + 3, 3))) { mp[ss.substr(i, 3)] = 1; dp[i][3] = 1; } else dp[i][3] = 0; } } cout << mp.size() << endl; for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) { cout << it->first << 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.*; import java.io.*; public class A { public static void main(String[] args) throws Exception { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); char[] s = in.next().toCharArray(); boolean[][] dp = new boolean[2][s.length + 1]; dp[0][s.length] = true; dp[1][s.length] = true; TreeSet<String> result = new TreeSet<String>(); for(int x = s.length - 2; x >= 5; x--) { for(int y = 2; y <= 3 && x + y < dp[0].length; y++) { if(dp[1 - (y - 2)][x + y] || (dp[y - 2][x + y] && !equal(s, x, x + y, x + y, x + y + y))) { dp[y - 2][x] = true; StringBuilder sb = new StringBuilder(); for(int z = 0; z < y; z++) { sb.append(s[x + z]); } result.add(sb.toString()); } } } out.println(result.size()); for(String str : result) { out.println(str); } out.close(); } public static boolean equal(char[] s, int s1, int e1, int s2, int e2) { for(int x = s1; x < e1; x++) { if(s[x] != s[s2 + (x - s1)]) { return false; } } return true; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream input) { br = new BufferedReader(new InputStreamReader(input)); st = new StringTokenizer(""); } public String next() throws IOException { if(st.hasMoreTokens()) { return st.nextToken(); } else { st = new StringTokenizer(br.readLine()); return next(); } } public int nextInt() throws IOException { return Integer.parseInt(next()); } } }
JAVA