URAL 1989 Subpalindromes (palindrome string segment tree polynomial hash)

Source: Internet
Author: User

1989. Subpalindromestime limit:0.5 Second
Memory limit:64 MB
You have a string and queries of types:
    1. Replace i' th character of the string by character a;
    2. Check if substring sJ... s k is a palindrome.
Inputthe first line contains a string consisting of NSmall 中文版 Letters. The second line contains an integer mThat's the number of queries (5≤ N, m≤105). The next mlines contain the queries.
Each query have either form "change I A", or "palindrome?" J K ", where I, J, kis integers (1≤ IN; 1≤ JkN), and character ais a small 中文版 letter. OutputTo all second type queries, you should output "Yes" to a single line if substring s J... s kis a palindrome and "No" otherwise. Sample
input Output
Abcda5palindrome? 1 5palindrome? 1 1change 4 bpalindrome? 1 5palindrome? 2 4
Noyesyesyes
problem Author:Mikhail Rubinchik
problem Source:Open Ural FU Championship 2013
Tags:None()



This topic last night saw after midnight other people's problem solving report only to understand what meaning, original line tree is powerful to omnipotent. I want to write down the polynomial hash definition:

For any one interval l,l+1,..., R

keyl=str[L] +str[l+1]*k + str[l+2]* k^2 +...str[r] * k^ (R-L)

Keyr=str[r] +str[r-1]*k + str[r-2]* k^2 +...str[l] * k^ (R-L)

As long as the palindrome string, then Keyl and Keyr will be equal, K is a constant.


What does it have to do with a line tree? It would have been assumed that all leaf nodes were each of the polynomial counterpart, so that the middle node might be a subset of the polynomial. is not my intuitive understanding, but any one of the nodes are satisfied

That polynomial form, that is, any one node is str "L" + str[L + 1] * k + str[L + 2] * K^2 ...   This form, which is why the merger is assumed for Keyl right child to be multiplied by pow "x", X is the number of left child descendants. In addition, when asked to learn a trick, set a variable t, this t value is very interesting, if you are asked to walk down the process, if there is no left and right children's cross-traversal, then T = = 2 | | t = = 1, only when the need to merge around the time t = = 3, is not very clever processing? , how to represent the positive and inverse two-way hash results of each node from the data structure?

It is obvious that the struct or pair can be, and the past is to use the number of groups to represent the node information, this time to study the two properties of each node, then the structure or the pair inevitability out of it

The analysis of the code is very clear, the details of a look at the understanding:

/*=============================================================================## Author:liangshu-cbam # # QQ        : 756029571 # # School: Harbin Polytechnic University # # last modified:2015-08-08 17:18## filename:a.cpp## Description: # The people who is crazy enough to think they can change the world, is the ones who does! =============================================================================*/# #include <iostream> # include<sstream> #include <algorithm> #include <cstdio> #include <string.h> #include <c ctype> #include <string> #include <cmath> #include <vector> #include <stack> #includ e<queue> #include <map> #include <set> using namespace std; #define Lson L, M, Rt<<1#define  Rson m + 1, R, rt<<1|1const int k = 137;const int maxn = 100003;char str1[maxn];int pow[maxn];int str2[maxn];int N,    m;struct node{int Keyl,keyr;  Node (): Keyl (0), Keyr (0) {}  Node (int x, int y): Keyl (x), Keyr (y) {}}node[maxn<<2];void init () {for (int i = 1;i <= maxn<<2; i++) {    Node[i].keyl = Node[i].keyr = 0;    }}void pushup (int L, int R, int rt) {Node[rt].keyl = Node[rt<<1].keyl + node[rt<<1|1].keyl*pow[l]; Node[rt].keyr = Node[rt<<1].keyr * Pow[r] + Node[rt<<1|1].keyr;}    void build (int l, int r, int rt) {if (L = = r) {Node[rt].keyl = Node[rt].keyr = Str2[l];return;    } int m = (L + r) >>1;    Build (Lson);    Build (Rson); Pushup (m-l + 1, r-m, RT);}    void Update (int p, int val, int l, int r, int rt) {if (L = = r) {Node[rt].keyl = Node[rt].keyr = Val;return;    } int m = (L + r) >>1;    if (P <= m) update (p, Val, Lson);    else Update (p, Val, Rson); Pushup (m-l + 1, r-m, RT);}    Node query (int l, int R, int l, int r,int RT) {if (L <= l && R <= R) {return NODE[RT];    } Node ans, ans1, ans2;    int T = 0; int m = (L + r) &Gt;>1;    if (l <= m) ans1 = Query (L, R, Lson), T + = 1;        if (R > m) ans2 = Query (L, R, Rson), T + = 2;    if (T = = 1) ans = ans1;    else if (T = = 2) ans = ans2;        else if (T = = 3) {Ans.keyl = Ans1.keyl + Ans2.keyl * Pow[m-max (L, L) + 1];    Ans.keyr = Ans1.keyr * Pow[min (R, R)-m] + Ans2.keyr; } return ans;        int main () {while (~SCANF ("%s", str1 + 1)) {int L = strlen (str1 + 1);        Pow[0] = 1;        for (int i = 1; I <= L; i++) {pow[i] = pow[i-1] * k;        } for (int i = 1; I <= L; i++) {str2[i] = str1[i]-' a ';        } init ();        scanf ("%d", &m);        Build (1, L, 1);            for (int i = 1; I <= M; i++) {char op[23];            scanf ("%s", op);                if (op[0] = = ' C ') {int A;char b;                scanf ("%d%c", &a, &b);            Update (A, B-' a ', 1, L, 1);                } else {int A, B; scanf ("%d%d", &a, &b);                Node ans = Query (A, B, 1, L, 1);               BOOL T = Ans.keyl = = Ans.keyr;              if (T) printf ("yes\n");             else printf ("no\n"); }}} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

URAL 1989 Subpalindromes (palindrome string segment tree polynomial hash)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.