답안 #48351

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
48351 2018-05-11T19:17:18 Z octopuses 회문 (APIO14_palindrome) C++17
73 / 100
1000 ms 74384 KB
#include <bits/stdc++.h>

#define ll long long
#define fr first
#define sc second
#define M (ll)(3e17)

using namespace std;
const int N = 300020;

char tmp[N];
int n;
ll answer, ans;
int Suf[N][20], c[N], d[N], temp[N], cnt[N];
int A[N], B[N];
int P[N], C[N];
set < int > S;
string a;
vector < int > G[N];
vector < pair < int, int > > g[N];

int Parent(int v)
{
  if(P[v] == v)
    return v;
  return P[v] = Parent(P[v]);
}

void DSU(int v, int u)
{
  v = Parent(v);
  u = Parent(u);
  if(v == u)
    return;
  C[v] += C[u];
  P[u] = v;
  ans = max(ans, C[v] * 1ll);
}

int fnd(int mn, int v, int u)
{
  int s = 0;
  for(int k = 18; k >= 0; -- k)
    if(Suf[u][k] == Suf[v][k])
    {
      u += (1 << k);
      v += (1 << k);
      s += (1 << k);
    }
  s = min(mn, s);
  return s;
}

void sufsort()
{
  Suf[n + 1][0] = 0;
  int tot = 1;
  for(char ch = 'a'; ch <= 'z'; ++ ch)
    for(int i = 1; i <= n; ++ i)
      if(a[i] == ch)
        d[tot ++] = i;
  tot = 0;
  for(int i = 1; i <= n; ++ i)
  {
    if(a[d[i]] != a[d[i - 1]])
      tot ++;
    Suf[d[i]][0] = tot;
  }
  d[0] = n + 1;
  for(int i = 0; i <= n; ++ i)
    c[i] = d[i];
  for(int k = 1; k < 19; ++ k)
  {
    for(int i = 0; i <= n; ++ i)
      cnt[i] = 0;
    for(int i = 1; i <= n + 1; ++ i)
      cnt[Suf[i][k - 1]] ++;
    for(int i = 1; i <= n; ++ i)
      cnt[i] += cnt[i - 1];
    for(int i = n; i >= 0; -- i)
    {
      if(d[i] <= (1 << (k - 1)))
        continue;
      int j = d[i] - (1 << (k - 1));
      c[cnt[Suf[j][k - 1]] - 1] = j;
      cnt[Suf[j][k - 1]] --;
    }
    tot = 0;
    Suf[n + 1][k] = 0;
    for(int i = 1; i <= n; ++ i)
    {
      d[i] = c[i];
      if(Suf[c[i]][k - 1] != Suf[c[i - 1]][k - 1] || (c[i - 1] + (1 << (k - 1)) > n + 1) || Suf[c[i] + (1 << (k - 1))][k - 1] != Suf[c[i - 1] + (1 << (k - 1))][k - 1])
        tot ++;
      Suf[c[i]][k] = tot;
    }
  }
  for(int i = 1; i <= n; ++ i)
    c[d[i]] = i;
}

void palindrome()
{
  A[1] = 1;
  int r = 1;
  int l = 1;
  for(int i = 2; i <= n; ++ i)
  {
    A[i] = 1;
    if(r >= i)
      A[i] = min(A[l + r - i], r - i + 1);
    while(i + A[i] <= n && i - A[i] > 0 && a[i - A[i]] == a[i + A[i]])
      A[i] ++;
    if(i + A[i] - 1 > r)
      r = i + A[i] - 1, l = i - A[i] + 1;
  }
  B[1] = 0;
  l = r = 1;
  for(int i = 1; i <= n; ++ i)
  {
    B[i] = 0;
    if(r >= i)
      B[i] = min(B[l + r - i + 1], r - i + 1);
    while(i + B[i] <= n && i - B[i] - 1 > 0 && a[i + B[i]] == a[i - B[i] - 1])
      B[i] ++;
    if(i + B[i] - 1 > r)
      r = i + B[i] - 1, l = i - B[i];
  }
}

int main()
{
  scanf("%s", &tmp);
  n = strlen(tmp);
  a = '#' + string(tmp) + char('a' - 1);
  sufsort();
  palindrome();

// Odd Palindromes

  for(int i = 1; i <= n; ++ i)
    P[i] = i, C[i] = 0;
  for(int i = 1; i <= n; ++ i)
    G[A[i]].push_back(i);
  ans = 0;
  for(int k = n; k >= 1; -- k)
  {
    for(int i = 0; i < G[k].size(); ++ i)
    {
      int v = G[k][i];
      set < int>::iterator it = S.lower_bound(c[v]);
      if(it != S.end())
        g[fnd(k, v, d[(*it)])].push_back({v, d[(*it)]});
      if(it != S.begin())
      {
        it --;
        g[fnd(k, v, d[(*it)])].push_back({v, d[(*it)]});
      }
      S.insert(c[v]);
      C[v] = 1;
      ans = max(ans, 1ll);
    }
    for(int i = 0; i < g[k].size(); ++ i)
      DSU(g[k][i].first, g[k][i].second);
    answer = max(answer, ans * (2 * k - 1ll));
  }
//  Even Palindromes

  for(int i = 1; i <= n; ++ i)
    P[i] = i, C[i] = 0, G[i].clear(), g[i].clear();
  for(int i = 1; i <= n; ++ i)
    G[B[i]].push_back(i);
  S.clear();
  ans = 0;
  for(int k = n; k >= 1; -- k)
  {
    for(int i = 0; i < G[k].size(); ++ i)
    {
      int v = G[k][i];
      set < int>::iterator it = S.lower_bound(c[v]);
      if(it != S.end())
        g[fnd(k, v, d[(*it)])].push_back({v, d[(*it)]});
      if(it != S.begin())
      {
        it --;
        g[fnd(k, v, d[(*it)])].push_back({v, d[(*it)]});
      }
      S.insert(c[v]);
      C[v] = 1;
      ans = max(ans, 1ll);
    }
    for(int i = 0; i < g[k].size(); ++ i)
      DSU(g[k][i].first, g[k][i].second);
    answer = max(answer, ans * (2ll * k));
  }
  cout << answer;
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:133:19: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[300020]' [-Wformat=]
   scanf("%s", &tmp);
               ~~~~^
palindrome.cpp:148:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < G[k].size(); ++ i)
                    ~~^~~~~~~~~~~~~
palindrome.cpp:163:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < g[k].size(); ++ i)
                    ~~^~~~~~~~~~~~~
palindrome.cpp:177:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < G[k].size(); ++ i)
                    ~~^~~~~~~~~~~~~
palindrome.cpp:192:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < g[k].size(); ++ i)
                    ~~^~~~~~~~~~~~~
palindrome.cpp:133:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", &tmp);
   ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 14456 KB Output is correct
2 Correct 12 ms 14456 KB Output is correct
3 Correct 12 ms 14508 KB Output is correct
4 Correct 15 ms 14508 KB Output is correct
5 Correct 12 ms 14540 KB Output is correct
6 Correct 12 ms 14608 KB Output is correct
7 Correct 14 ms 14608 KB Output is correct
8 Correct 15 ms 14608 KB Output is correct
9 Correct 13 ms 14672 KB Output is correct
10 Correct 14 ms 14672 KB Output is correct
11 Correct 12 ms 14672 KB Output is correct
12 Correct 12 ms 14672 KB Output is correct
13 Correct 13 ms 14672 KB Output is correct
14 Correct 13 ms 14676 KB Output is correct
15 Correct 12 ms 14676 KB Output is correct
16 Correct 13 ms 14696 KB Output is correct
17 Correct 15 ms 14696 KB Output is correct
18 Correct 12 ms 14696 KB Output is correct
19 Correct 13 ms 14696 KB Output is correct
20 Correct 14 ms 14696 KB Output is correct
21 Correct 16 ms 14696 KB Output is correct
22 Correct 13 ms 14752 KB Output is correct
23 Correct 13 ms 14752 KB Output is correct
24 Correct 13 ms 14752 KB Output is correct
25 Correct 12 ms 14752 KB Output is correct
26 Correct 12 ms 14752 KB Output is correct
27 Correct 12 ms 14752 KB Output is correct
28 Correct 12 ms 14752 KB Output is correct
29 Correct 12 ms 14752 KB Output is correct
30 Correct 12 ms 14752 KB Output is correct
31 Correct 13 ms 14752 KB Output is correct
32 Correct 12 ms 14752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14820 KB Output is correct
2 Correct 13 ms 14824 KB Output is correct
3 Correct 13 ms 14824 KB Output is correct
4 Correct 13 ms 14948 KB Output is correct
5 Correct 13 ms 14948 KB Output is correct
6 Correct 13 ms 14948 KB Output is correct
7 Correct 13 ms 14948 KB Output is correct
8 Correct 13 ms 14948 KB Output is correct
9 Correct 13 ms 14948 KB Output is correct
10 Correct 14 ms 14948 KB Output is correct
11 Correct 13 ms 14948 KB Output is correct
12 Correct 14 ms 14948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 16740 KB Output is correct
2 Correct 23 ms 16740 KB Output is correct
3 Correct 28 ms 16740 KB Output is correct
4 Correct 26 ms 16740 KB Output is correct
5 Correct 25 ms 16740 KB Output is correct
6 Correct 25 ms 16740 KB Output is correct
7 Correct 24 ms 16740 KB Output is correct
8 Correct 24 ms 16740 KB Output is correct
9 Correct 26 ms 16740 KB Output is correct
10 Correct 26 ms 16740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 148 ms 34396 KB Output is correct
2 Correct 148 ms 34396 KB Output is correct
3 Correct 216 ms 34396 KB Output is correct
4 Correct 195 ms 34396 KB Output is correct
5 Correct 189 ms 34396 KB Output is correct
6 Correct 166 ms 34396 KB Output is correct
7 Correct 178 ms 34396 KB Output is correct
8 Correct 225 ms 34396 KB Output is correct
9 Correct 200 ms 34396 KB Output is correct
10 Correct 216 ms 34396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 585 ms 74384 KB Output is correct
2 Correct 558 ms 74384 KB Output is correct
3 Correct 752 ms 74384 KB Output is correct
4 Correct 713 ms 74384 KB Output is correct
5 Execution timed out 1089 ms 74384 KB Time limit exceeded
6 Halted 0 ms 0 KB -