답안 #941226

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
941226 2024-03-08T10:23:20 Z wii 회문 (APIO14_palindrome) C++17
100 / 100
36 ms 69092 KB
#include <bits/stdc++.h>
using namespace std;

typedef double db;
typedef long long ll;
typedef long double ld;

#define int ll
typedef pair<int, int> pii;

#define lx (id << 1)
#define rx (lx | 1)
#define gcd __gcd
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define bit(i, mask) ((mask) >> (i) & 1)
#define reset(x, val) memset(x, val, sizeof(x))
#define foru(i,a,b) for(int i = (a); i <= (b); ++i)
#define ford(i,a,b) for(int i = (a); i >= (b); --i)
#define FastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; }

const ll Linf = 0x3f3f3f3f3f3f3f3f;
const int Inf = 0x3f3f3f3f;
const ll Mod = 1e9 + 7;
const ll Mod2 = ll(1e9) + 9;
const int Lim = 1e6 + 5;
const int inv6 = 166666668;

/// ====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====

const int base = 3;
const int N = 3e5 + 5;
const int K = log2(N) + 1;
const int dx[] = {+1, -1, 0, 0};
const int dy[] = {0, 0, +1, -1};
const int block_size = sqrt(2e9) + 2;

struct Node {
    int len;
    int suffLink;
    int num;
    int nxt[26];
};

int num;
int len;
int suff;
Node tree[N];
string str;

bool addLetter(int pos) {
    int cur = suff, curLen = 0;
    int let = str[pos] - 'a';

    while (true) {
        curLen = tree[cur].len;
        if (pos - 1 - curLen >= 0 && str[pos - 1 - curLen] == str[pos])
            break;
        cur = tree[cur].suffLink;
    }

    if (tree[cur].nxt[let]) {
        suff = tree[cur].nxt[let];
        return false;
    }

    ++num;
    suff = num;
    tree[num].len = tree[cur].len + 2;
    tree[cur].nxt[let] = num;

    if (tree[num].len == 1) {
        tree[num].suffLink = 2;
        return true;
    }

    while (true) {
        cur = tree[cur].suffLink;
        curLen = tree[cur].len;

        if (pos - 1 - curLen >= 0 && str[pos - 1 - curLen] == str[pos]) {
            tree[num].suffLink = tree[cur].nxt[let];
            break;
        }
    }

    return true;
}

void solve() {
    cin >> str;

    num = 2; suff = 2;
    tree[1].len = -1; tree[2].suffLink = 1;
    tree[2].len = 0; tree[2].suffLink = 1;

    for (int i = 0; i < str.size(); ++i) {
        addLetter(i);
        ++tree[suff].num;
    }

    int ans = 0;
    for (int i = num; i >= 3; --i) {
        tree[tree[i].suffLink].num += tree[i].num;
        maximize(ans, tree[i].len * tree[i].num);

    }

    cout << ans;
}

signed main() {
    FastIO;

    #define task "test"
    if (fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}

    #define task "palindrome"
    if (fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}

    int ntest = 1;
    // cin >> ntest;
    while (ntest--) {
        //cout << "Case " << q << ": " << "\n";
        solve();
        cout << "\n";
    }

    return 0;
}

/**  /\_/\
 *  (= ._.)
 *  / >TL \>AC
**/

Compilation message

palindrome.cpp:124: warning: "task" redefined
  124 |     #define task "palindrome"
      | 
palindrome.cpp:118: note: this is the location of the previous definition
  118 |     #define task "test"
      | 
palindrome.cpp: In function 'void solve()':
palindrome.cpp:100:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     for (int i = 0; i < str.size(); ++i) {
      |                     ~~^~~~~~~~~~~~
palindrome.cpp: In function 'int main()':
palindrome.cpp:120:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:121:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:126:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:127:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 600 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 0 ms 692 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4696 KB Output is correct
2 Correct 1 ms 4668 KB Output is correct
3 Correct 1 ms 4772 KB Output is correct
4 Correct 2 ms 4700 KB Output is correct
5 Correct 2 ms 4888 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 568 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 23384 KB Output is correct
2 Correct 6 ms 23388 KB Output is correct
3 Correct 4 ms 23388 KB Output is correct
4 Correct 5 ms 23388 KB Output is correct
5 Correct 8 ms 23388 KB Output is correct
6 Correct 7 ms 16988 KB Output is correct
7 Correct 5 ms 19804 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 3 ms 7004 KB Output is correct
10 Correct 5 ms 21340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 68836 KB Output is correct
2 Correct 24 ms 69080 KB Output is correct
3 Correct 18 ms 69088 KB Output is correct
4 Correct 18 ms 69092 KB Output is correct
5 Correct 36 ms 69084 KB Output is correct
6 Correct 19 ms 62692 KB Output is correct
7 Correct 18 ms 57824 KB Output is correct
8 Correct 5 ms 3296 KB Output is correct
9 Correct 5 ms 3300 KB Output is correct
10 Correct 19 ms 58796 KB Output is correct
11 Correct 18 ms 69092 KB Output is correct
12 Correct 6 ms 9440 KB Output is correct