Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <array>
- #include <vector>
- #include <numeric>
- #include <random>
- #include <chrono>
- using namespace std;
- #define int long long
- #pragma comment(linker,"/STACK:1000000000,1000000000")
- const long long INF = 1e9 + 7;
- const int MAXN = 1e4 + 5;
- const int N = 1e5 + 10;
- struct vertex {
- int next[26];
- bool terminal = false;
- } v[MAXN];
- int root = 0, top = 1;
- void add(const string& s) {
- int cur = root;
- for (auto& i: s) {
- int x = i - 'a';
- if (v[cur].next[x] == 0) {
- v[cur].next[x] = top++;
- }
- cur = v[cur].next[x];
- }
- v[cur].terminal = true;
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- string s;
- cin >> s;
- int n = s.size();
- for (int len = 1; len <= n; ++len) {
- for (int l = 0; l + len - 1 < n; ++l) {
- add(s.substr(l, len));
- }
- }
- int ans = 0;
- for (int i = 0; i < top; ++i) {
- ans += v[i].terminal;
- }
- cout << ans << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment