Line tree-hdu-4339-Query

Source: Internet
Author: User

Question meaning:

Give two strings. There are two types of operations.

1. Change a character at a certain position of a string.

2. Find the maximum number of two consecutive identical characters at the beginning of a position.

Solution:

The line segment tree maintains two values: the leftmost continuous maximum value of the interval and the rightmost continuous maximum value.

Every question is made, so we can stop thinking and abstract a little bit of experience.

Code:

 

# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <set> # include <stack> # include <list> # include <queue> # define eps 1e-6 # define INF 0x1f1f1f1f # define PI acos (-1.0) # define ll _ int64 # define lson l, m, (rt <1) # define rson m + 1, r, (rt <1) | 1 // # pragma comment (linker, "/STACK: 1024000000,1024000000") using names Pace std; # define Maxn 1100000/* freopen ("data. in "," r ", stdin); freopen (" data. out "," w ", stdout); */struct Node {int ls, rs;} node [Maxn * 4]; // The maintenance interval of the Line Segment tree, 1. including the maximum number of consecutive equal numbers at the left endpoint, // 2. the maximum number of consecutive equal numbers at the right endpoint. char sa1 [Maxn], sa2 [Maxn]; int nm, n; void pushup (int rt, int num) // update {node [rt] upwards. ls = node [rt <1]. ls; node [rt]. rs = node [rt <1 | 1]. rs; if (node [rt]. ls> = num-(num> 1) // The number of left child ranges is num-(num> 1) node [rt]. ls + = node [rt <1 | 1]. ls; if (Node [rt]. rs> = (num> 1) // The number of right child ranges is num> 1 node [rt]. rs + = node [rt <1]. rs;} void build (int l, int r, int rt) {if (l = r) {if (l <nm) // The minimum length of the two strings node [rt]. ls = node [rt]. rs = (sa1 [l] = sa2 [r])? 1:0; else node [rt]. ls = node [rt]. rs = 0; return;} int m = (l + r)> 1; build (lson); build (rson); pushup (rt, r-l + 1);} void update (int k, int l, int r, int rt) {if (l = r) {if (l <nm) node [rt]. ls = node [rt]. rs = (sa1 [l] = sa2 [r])? 1:0; else node [rt]. ls = node [rt]. rs = 0; return;} int m = (l + r)> 1; if (k <= m) update (k, lson); else update (k, rson); pushup (rt, r-l + 1);} int query (int k, int l, int r, int rt) {if (l = r) return 1; // The position must be the same int m = (l + r)> 1; if (k <= m) {if (node [rt <1]. rs> m-k) // return m-k + 1 + node [rt <1 | 1]. ls; else return query (k, lson);} else return query (k, rson);} int main () {// scanf ("% s", sa1 ); // scanf ("% s", sa1 );// Printf ("% c \ n", sa1 [5]); int t; int AB, po, m, ki; char s [5]; scanf ("% d ", & t); for (int ca = 1; ca <= t; ca ++) {scanf ("% s", sa1); scanf ("% s ", sa2); int len1 = strlen (sa1), len2 = strlen (sa2); n = max (len1, len2); nm = min (len1, len2); build (0, n-1, 1); printf ("Case % d: \ n", ca); scanf ("% d", & m); while (m --) {scanf ("% d", & ki); if (ki = 1) {scanf ("% d % s", & AB, & po, s ); if (AB = 1) sa1 [po] = * s; else sa2 [po] = * s; update (po, 0, n-1, 1);} els E {scanf ("% d", & po); if (po> = nm | sa1 [po]! = Sa2 [po]) {printf ("0 \ n"); continue;} printf ("% d \ n", query (po, 0, n-1, 1); }}return 0 ;}

 

Related Article

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.