Topic Portal
Test instructions: Give two strings, action 1: Replace the character action of one of the strings in a position 2: Query the longest continuous length equal to the beginning of P
Analysis: A tree array can maintain a common length (continuous) within an interval, and the query uses two points to find the farthest endpoint. can also use line segment tree to do, line tree can deal with a lot of problems, this problem as long as the right zone to merge on the line.
Harvest: 1. The interval of the segment tree is also a problem (although I have not written AC) 2. The tree-like array is easy to write and miraculous!
Code 1 (tree-like array):
/************************************************* author:running_time* Created time:2015-8-26 9:46:09* File Nam E:i.cpp ************************************************/#include <cstdio> #include <algorithm># Include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 1e6 + 10;const int Q = 1e5 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;cha R S1[n], S2[n];int l;struct BIT {int c[n]; void init (void) {memset (c, 0, sizeof (c)); } void Updata (int i, int x) {while (I <= L) {C[i] + = x; i + = (I & (i)); }} int query (int i) {int ret = 0; while (i) {ret + = C[i]; I-= (I & (i)); } return ret; } int bsearch (int p, int l, int r) {while (L <= r) {int mid = (L + r) >> 1; if (query (mid)-query (p-1) = = mid-p + 1) L = mid + 1; else R = mid-1; } return l-p; }}bit;int Main (void) {int T, cas = 0; scanf ("%d", &t); while (t--) {scanf ("%s%s", S1 + 1, s2 + 1); int q; scanf ("%d", &q); int len1 = strlen (S1 + 1), len2 = strlen (s2 + 1); L = min (len1, len2); Bit.init (); for (int i=1; i<=l; ++i) {if (s1[i] = = S2[i]) bit.updata (i, 1); } printf ("Case%d:\n", ++cas); while (q--) {int op; scanf ("%d", &op); if (op = = 1) {int x, p; char c; scanf ("%d%d%c", &x, &p, &c); ++p; if (P > L) continue; bool same = (s1[p] = = S2[p]); if (x = = 1) {s1[p] = C; if (same && s1[p]! = S2[p]) bit.updata (p,-1); else if (!same && s1[p] = = S2[p]) bit.updata (p, 1); } else {s2[p] = C; if (same && s1[p]! = S2[p]) bit.updata (p,-1); else if (!same && s1[p] = = S2[p]) bit.updata (p, 1); }} else {int p; scanf ("%d", &p); ++p; if (P > L) {puts ("0"); continue; } printf ("%d\n", Bit.bsearch (p, p, L)); }}} return 0;}
Code 2 (segment tree):
/************************************************* author:running_time* Created time:2015-8-26 9:46:09* File Nam E:i.cpp ************************************************/#include <cstdio> #include <algorithm># Include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 1e6 + 10;const int Q = 1e5 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;cha R S1[n], s2[n];struct ST {int sum[n<<2]; void push_up (int rt, int len) {//sum[rt<<1] represents the longest continuous public length starting from the left end of the left subtree segment, if all is public, if (sum[rt<<1] = = Len-(Len >> 1)) Sum[rt] = sUM[RT<<1] + sum[rt<<1|1]; Merges the segment length of the right subtree else sum[rt] = sum[rt<<1]; } void Build (int l, int r, int rt) {if (L = = r) {Sum[rt] = s1[l] = = S2[r]? 1:0; return; } int mid = (L + r) >> 1; Build (Lson); Build (Rson); Push_up (RT, r-l + 1); } void Updata (int p, int c, int l, int r, int rt) {if (L = = r) {Sum[rt] = C; return; } int mid = (L + r) >> 1; if (P <= mid) Updata (P, C, Lson); Else Updata (P, C, Rson); Push_up (RT, r-l + 1); } int query (int p, int l, int r, int rt) {if (L = = r) return SUM[RT]; int mid = (L + r) >> 1; if (P <= mid) {int ret = query (P, Lson); if (p + ret-1 = = Mid) return ret + sum[rt<<1|1]; else return ret; } else return query (P, Rson); }}st;int Main (void) {int T, CAs = 0; scanf ("%d", &t); while (t--) {scanf ("%s%s", S1 + 1, s2 + 1); int q; scanf ("%d", &q); int len1 = strlen (S1 + 1), len2 = strlen (s2 + 1); int mn = min (len1, len2); St.build (1, MN, 1); printf ("Case%d:\n", ++cas); while (q--) {int op; scanf ("%d", &op); if (op = = 1) {int x, p; char c; scanf ("%d%d%c", &x, &p, &c); ++p; if (P > Mn) continue; Special circumstances, if (x = = 1) {s1[p] = C; } else {s2[p] = C; } st.updata (P, (s1[p] = = S2[p]), 1, MN, 1); } else {int p; scanf ("%d", &p); ++p; if (P > Mn) {//Special case puts ("0"); continue; } printf ("%D\n ", St.query (P, 1, MN, 1)); }}} return 0;}
Code 3 (UNAC):
/************************************************* author:running_time* Created time:2015-8-26 9:46:09* File Nam E:i.cpp ************************************************/#include <cstdio> #include <algorithm># Include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 1e6 + 10;const int Q = 1e5 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;cha R S1[n], s2[n];struct ST {int sum[n<<2], lsum[n<<2], rsum[n<<2]; void push_up (int rt, int len) {Lsum[rt] = lsum[rt<<1]; RSUM[RT] = rsum[rt<<1|1]; Sum[RT] = Max (sum[rt<<1], sum[rt<<1|1]); if (lsum[rt] = = Len-(len >> 1)) Lsum[rt] + = lsum[rt<<1|1]; if (rsum[rt] = = Len >> 1) rsum[rt] + = rsum[rt<<1]; SUM[RT] = max (rsum[rt<<1] + lsum[rt<<1|1], Max (sum[rt<<1], sum[rt<<1|1])); } void Build (int l, int r, int rt) {if (L = = r) {Sum[rt] = Lsum[rt] = Rsum[rt] = s1[l] = = S2[r] ? 1:0; return; } int mid = (L + r) >> 1; Build (Lson); Build (Rson); Push_up (RT, r-l + 1); } void Updata (int p, int c, int l, int r, int rt) {if (L = = r) {Sum[rt] = Lsum[rt] = Rsum[rt] = C return; } int mid = (L + r) >> 1; if (P <= mid) Updata (P, C, Lson); Else Updata (P, C, Rson); Push_up (RT, r-l + 1); } int query (int p, int l, int r, int rt) {if (L = = r) return SUM[RT]; int mid = (L + r) >>1; if (P <= mid) {if (P + rsum[rt<<1]-1 >= mid) return mid-p + 1 + query (p, Rson); else return query (P, Lson); } else return query (P, Lson); }}st;int Main (void) {int T, cas = 0; scanf ("%d", &t); while (t--) {scanf ("%s%s", S1 + 1, s2 + 1); int q; scanf ("%d", &q); int len1 = strlen (S1 + 1), len2 = strlen (s2 + 1); int mn = min (len1, len2); St.build (1, MN, 1); printf ("Case%d:\n", ++cas); while (q--) {int op; scanf ("%d", &op); if (op = = 1) {int x, p; char c; scanf ("%d%d%c", &x, &p, &c); if (p + 1 > MN) continue; if (x = = 1) {s1[p+1] = C; } else {s2[p+1] = C; } st.updata (P+1, (s1[p+1] = = S2[p+1]), 1, MN, 1); } else { int p; scanf ("%d", &p); if (p + 1 > Mn) {puts ("0"); continue; } printf ("%d\n", St.query (P + 1, 1, MN, 1)); }}} return 0;}
binary + Tree Array/segment tree (interval update) hdoj 4339 Query