Subpalindromes, palindrome

Source: Internet
Author: User

Subpalindromes, palindrome

Question Link

  • Question:
    Give a string (length cannot exceed 1 E5), m operations (m cannot exceed 1 E5), each operation: 1. Query [l, r] whether it is a text string. 2. Change the p position to v.
  • Analysis:
    This topic involves a processing method of the return string: To determine whether the forward and reverse are the same, a simple method is to use Hash to identify. The subsequent problems are vertex update and query interval, which are solved by the line segment tree.
const int MAXN = 410000;const int SEED = 13331;ULL f[MAXN];char ipt[MAXN], t[110];#define lson rt << 1#define rson rt << 1 | 1struct Node{    int l, r, m;    ULL vl, vr;} nd[MAXN << 2], tt;void merge(Node& ret, Node& l, Node& r, int L, int M, int R){    ret.vl = r.vl * f[M - L + 1] + l.vl;    ret.vr = l.vr * f[R - M] + r.vr;}void pushup(int rt){    merge(nd[rt], nd[lson], nd[rson], nd[rt].l, nd[rt].m, nd[rt].r);}void build(int l, int r, int rt){    nd[rt].l = l; nd[rt].r = r; nd[rt].m = (r + l) >> 1;    if (l == r)    {        nd[rt].vl = nd[rt].vr = ipt[l - 1];    }    else    {        build(l, nd[rt].m, lson);        build(nd[rt].m + 1, r, rson);        pushup(rt);    }}void update(int p, int v, int rt){    if (nd[rt].l == nd[rt].r)    {        nd[rt].vl = nd[rt].vr = v;    }    else    {        if (p <= nd[rt].m)            update(p, v, lson);        else            update(p, v, rson);        pushup(rt);    }}Node query(int L, int R, int rt){    if (L <= nd[rt].l && nd[rt].r <= R)        return nd[rt];    if (R <= nd[rt].m)        return query(L, R, lson);    else if (L > nd[rt].m)        return query(L, R, rson);    Node tl = query(L, R, lson);    Node tr = query(L, R, rson);    Node ret;    merge(ret, tl, tr, max(L, nd[rt].l), nd[rt].m, min(R, nd[rt].r));    return ret;}int main(){    f[0] = 1;    FF(i, 1, MAXN)        f[i] = f[i - 1] * SEED;    int n;    while (~RS(ipt))    {        build(1, strlen(ipt), 1);        RI(n);        REP(i, n)        {            RS(t);            if (t[0] == 'p')            {                int a, b;                RII(a, b);                tt = query(a, b, 1);                printf("%s\n", tt.vl == tt.vr ? "Yes" : "No");            }            else            {                int p; char v;                scanf("%d %c", &p, &v);                update(p, v, 1);            }        }    }    return 0;}



How to read Palindromes?

Pal-in-dromes: Can you tell me what it means? Follow the feeling to read

C ++ programming for return and prime numbers

Not difficult.
It only requires no array, no string
A function must be used. The function must be placed in the header file and must be called.

No need to use arrays...
Create a header file and create two functions in it. Both functions have only one parameter. The returned value can be 0/1 or another.
Function 1 Implementation: Check whether a number is a return number. Use the remainder calculation method to check whether the first and last bits are the same. If they are different, the function returns a number, check whether the input parameter is a return value.
Function 2 Implementation: Check whether a number is a prime number. I don't need to talk about this.

Finally, create the main function to call the original header file.
A loop in the main function, 1--N
You can use two functions to view them one by one. The values that meet the conditions are the required numbers.

Finished ..... If arrays are used, the idea may be clearer.

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.