ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1635. Мнемоника и палиндромы

hint
Послано ASK 21 ноя 2010 22:06
Build a data-structure which allows O(1) test that s[from..to] is a palindrome. The structure is a 2 x s.size() array: even/odd length times possible center point.

typedef vector<int> V;
string s(4001, ' ');
int n;
V mpl[2]; // maximum palindrome length around i (even and odd lengths)
inline bool is_polindrome(int from, int to){
  assert(0 <= from and from < to and to <= n);
  int eo = (to - from) % 2, c = (from + to) / 2;
  return mpl[eo][c] >= c - from;
}
Re: hint
Послано marat 12 апр 2011 03:53
One can think like that:
Let a(k) be the amount of palindromes in [0,k]. Then we have recursion:
a(k)=MIN{ 1 if [0,k] is pm  , MIN (from i = 0 to k -1) OF a(i) + 1   if [i,k] is pm}
        { 0 else                                          a(i) + k-i else          }

I consider ASK to be absolutely right concerning data structure He proposed.

Re: hint
Послано net 18 июл 2011 15:12
And implemntataion by this idea =)
http://pastebin.com/6bGHLipR
Re: hint
Послано chomu1850 22 ноя 2015 20:44
Considering each character as a pivot, expand on both sides to find the length of both even and odd length palindromes centered at the pivot character under consideration and store the length in the 2 arrays (odd & even).
Time complexity for this step is O(n^2)