首页 > 代码库 > URAL 1297 Palindrome (后缀数组+RMQ)

URAL 1297 Palindrome (后缀数组+RMQ)

题意:给定一个字符串,求一个最长的回回文子串,多解输出第一个。

析:把字符串翻转然后放到后面去,中间用另一个字符隔开,然后枚举每一个回文串的的位置,对第 i 个位置,那么对应着第二个串的最长公共前缀,

求最长公共子串,可以用RMQ解决。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2000 + 10;
const int mod = 1000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}

struct Array{
  int s[maxn], sa[maxn], t[maxn], t2[maxn];
  int h[maxn], r[maxn], c[maxn];
  int n;
  int dp[maxn][20];

  void init(){ n = 0;  memset(sa, 0, sizeof sa); }
  void build_sa(int m){
    int *x = t, *y = t2;
    for(int i = 0; i < m; ++i)  c[i] = 0;
    for(int i = 0; i < n; ++i)  ++c[x[i] = s[i]];
    for(int i = 1; i < m; ++i)  c[i] += c[i-1];
    for(int i = n-1; i >= 0; --i)  sa[--c[x[i]]] = i;

    for(int k = 1; k <= n; k <<= 1){
      int p = 0;
      for(int i = n-k; i < n; ++i)  y[p++] = i;
      for(int i = 0; i < n; ++i)  if(sa[i] >= k)  y[p++] = sa[i] - k;
      for(int i = 0; i < m; ++i)  c[i] = 0;
      for(int i = 0; i < n; ++i)  ++c[x[y[i]]];
      for(int i = 1; i < m; ++i)  c[i] += c[i-1];
      for(int i = n-1; i >= 0; --i)  sa[--c[x[y[i]]]] = y[i];

      swap(x, y);
      p = 1;  x[sa[0]] = 0;
      for(int i = 1; i < n; ++i)
        x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p-1 : p++;
      if(p >= n)  break;
      m = p;
    }
  }

  void getHight(){
    int k = 0;
    for(int i = 0; i < n; ++i)  r[sa[i]] = i;
    for(int i = 0; i < n; ++i){
      if(k)  --k;
      int j = sa[r[i]-1];
      while(s[i+k] == s[j+k])  ++k;
      h[r[i]] = k;
    }
  }

  void rmq_init(){
    for(int i = 1; i <= n; ++i)  dp[i][0] = h[i];
    for(int j = 1; (1<<j) <= n; ++j)
      for(int i = 1; i + (1<<j) <= n; ++i)
        dp[i][j] = min(dp[i][j-1], dp[i+(1<<j-1)][j-1]);
  }

  int query(int L, int R){
    L = r[L], R = r[R];
    if(L > R)  swap(L, R);
    ++L;
    int k = int(log(R-L+1) / log(2.0));
    return min(dp[L][k], dp[R-(1<<k)+1][k]);
  }
};
char s[maxn];
Array arr;

int main(){
  while(scanf("%s", s) == 1){
    n = strlen(s);
    arr.init();
    for(int i = 0; i < n; ++i)  arr.s[arr.n++] = s[i];
    arr.s[arr.n++] = 128;
    for(int i = n-1; i >= 0; --i) arr.s[arr.n++] = s[i];
    arr.s[arr.n++] = 0;
    arr.build_sa(130);
    arr.getHight();
    arr.rmq_init();
    int ans = 0, idx;
    for(int i = 0; i < n; ++i){
      int res = max(arr.query(i, 2*n-i) * 2 - 1, arr.query(i, 2*n-i+1) * 2); // odd
      if(res > ans){
        ans = res;  idx = i;
      }
    }
    if(ans & 1)  for(int i = idx-ans/2; i <= ans/2+idx; ++i)
      printf("%c", s[i]);
    else for(int i = idx-ans/2; i < idx+ans/2; ++i)
      printf("%c", s[i]);
    printf("\n");
  }
  return 0;
}

  

URAL 1297 Palindrome (后缀数组+RMQ)