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;
}

  

时间: 2025-01-18 08:59:11

URAL 1297 Palindrome (后缀数组+RMQ)的相关文章

URAL 1297 Palindrome 后缀数组

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just received a rather alarming anonymous letter. It states that the agent from the competing ?Robots Unlimited? has infiltrated into "U.S. Robotics".

URAL 1297. Palindrome(后缀数组求最大回文串)

题目大意:给你一串字符串,让你求出来它存在的最长连续的回文串. 解题思路:先把字符串逆序加到数组中,然后用后缀数组求解.两种方法:1,枚举排名,直接比较rank相同的字符串的位置差是不是len.如果是的话,就记录求解:2,枚举地址,求第i地址与第2*len-i+1的lcp的最大值. PS:需要注意如果多解输出靠前的字符串. 两种写法写在了一起,分别是Del,和Del1函数. 1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB T

URAL - 1297 Palindrome(后缀数组求最长回文子串)

Description The "U.S. Robots" HQ has just received a rather alarming anonymous letter. It states that the agent from the competing ?Robots Unlimited? has infiltrated into "U.S. Robotics". ?U.S. Robots? security service would have alrea

【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过

题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=1770 题解:一开始理解错题意,以为是abcxxxcba(xxx为v),开心地打了后缀数组后发现哎样例不对丫.. UVA的意思是abcxxxabc(xxx为v). 类似poj3693,我们暴

BZOJ 题目3172: [Tjoi2013]单词(AC自动机||AC自动机+fail树||后缀数组暴力||后缀数组+RMQ+二分等五种姿势水过)

3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 1890  Solved: 877 [Submit][Status][Discuss] Description 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. Input 第一个一个整数N,表示有多少个单词,接下来N行每行一个单词.每个单词由小写字母组成,N<=200,单词长度不超过10^6

URAL 1297. Palindrome(输出最长回文子串--后缀数组)

Input The input consists of a single line, which contains a string of Latin alphabet letters (no other characters will appear in the string). String length will not exceed 1000 characters. Output The longest substring with mentioned property. If ther

URAL 1297. Palindrome(后缀数组 求最长回文子串)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1297 1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just received a rather alarming anonymous letter. It states that the agent from the competing ?Robots

Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots»

URAL 1297 Palindrome(后缀数组+ST表)

[题目链接] http://acm.timus.ru/problem.aspx?num=1297 [题目大意] 求最长回文子串,并输出这个串. [题解] 我们将原串倒置得到一个新的串,加一个拼接符将新串拼在原串的后面, 那么枚举对称的中心点, 在两个串在组合成的串的对应位置的后缀的最长公共前缀 就是该点像两边扩展的最长回文子串的一半长度. 那么如何求任意两个后缀的最长公共前缀呢,考虑后缀数组的h数组和rank数组, 我们可以发现,两个后缀的最长公共前缀就是他们名次之间的h数组的最小值. 对h数组