답안 #112117

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
112117 2019-05-17T12:44:56 Z aleksami Palindromic Partitions (CEOI17_palindromic) C++14
100 / 100
167 ms 24980 KB
#include <bits/stdc++.h>

using namespace std;
#define MAXN 1000005
typedef long long ll;
const ll p = 131;
const ll MOD = 1e9+7;
ll addmod(ll x,ll y)
{
  x+=y;
  x+=MOD;
  x%=MOD;
  return x;
}
ll mulmod(ll x,ll y)
{
  x*=y;
  x%=MOD;
  return x;
}

ll pw[MAXN];
ll invpw[MAXN];

ll fastpow(ll x,int s)
{
  if(s==0)return 1LL;
  if(s%2==0)
  {
    ll y = fastpow(x,s/2);
    return mulmod(y,y);
  }
  return mulmod(x,fastpow(x,s-1));
}

void init()
{
  pw[0]=1LL;
  for(int i = 1; i < MAXN; i++)pw[i]=mulmod(pw[i-1],p);
  invpw[0]=1LL;
  ll invp = fastpow(p,MOD-2);
  for(int i = 1; i < MAXN; i++)invpw[i]=mulmod(invpw[i-1],invp);
}
ll sum[MAXN];
string s;
int n;

ll geth(ll x,int s)
{
  return mulmod(x,pw[s]);
}
ll get(int l,int r)
{
  ll ret = sum[r];
  if(l>0)ret=addmod(ret,-sum[l-1]);
  ret=mulmod(ret,invpw[l]);
  return ret;
}

int solve(int left,int right)
{
  for(int d = 1; left+d-1 < right-d+1; d++)
  {
    if(get(left,left+d-1)==get(right-d+1,right))return 2+solve(left+d,right-d);
  }
  if(left <= right)return 1;
  return 0;
}

void solve()
{
  cin >> s;
  n = s.length();
  for(int i = 0; i < n; i++)
  {
    sum[i]=geth(s[i],i);
    if(i>0)sum[i]=addmod(sum[i],sum[i-1]);
  }
  cout << solve(0,n-1) << "\n";
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  init();
  int t;
  cin >> t;
  while(t--)
  {
    solve();
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 16000 KB Output is correct
2 Correct 21 ms 15996 KB Output is correct
3 Correct 22 ms 15992 KB Output is correct
4 Correct 21 ms 16000 KB Output is correct
5 Correct 21 ms 16000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 16000 KB Output is correct
2 Correct 21 ms 15996 KB Output is correct
3 Correct 22 ms 15992 KB Output is correct
4 Correct 21 ms 16000 KB Output is correct
5 Correct 21 ms 16000 KB Output is correct
6 Correct 21 ms 15996 KB Output is correct
7 Correct 22 ms 15992 KB Output is correct
8 Correct 21 ms 15976 KB Output is correct
9 Correct 21 ms 15972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 16000 KB Output is correct
2 Correct 21 ms 15996 KB Output is correct
3 Correct 22 ms 15992 KB Output is correct
4 Correct 21 ms 16000 KB Output is correct
5 Correct 21 ms 16000 KB Output is correct
6 Correct 21 ms 15996 KB Output is correct
7 Correct 22 ms 15992 KB Output is correct
8 Correct 21 ms 15976 KB Output is correct
9 Correct 21 ms 15972 KB Output is correct
10 Correct 23 ms 16100 KB Output is correct
11 Correct 22 ms 16084 KB Output is correct
12 Correct 23 ms 16128 KB Output is correct
13 Correct 23 ms 16128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 16000 KB Output is correct
2 Correct 21 ms 15996 KB Output is correct
3 Correct 22 ms 15992 KB Output is correct
4 Correct 21 ms 16000 KB Output is correct
5 Correct 21 ms 16000 KB Output is correct
6 Correct 21 ms 15996 KB Output is correct
7 Correct 22 ms 15992 KB Output is correct
8 Correct 21 ms 15976 KB Output is correct
9 Correct 21 ms 15972 KB Output is correct
10 Correct 23 ms 16100 KB Output is correct
11 Correct 22 ms 16084 KB Output is correct
12 Correct 23 ms 16128 KB Output is correct
13 Correct 23 ms 16128 KB Output is correct
14 Correct 167 ms 24980 KB Output is correct
15 Correct 87 ms 24848 KB Output is correct
16 Correct 140 ms 24952 KB Output is correct
17 Correct 88 ms 20760 KB Output is correct