[BZOJ1789] [BZOJ1830] [Ahoi2008] Necklace Y-Necklace

Source: Internet
Author: User

[BZOJ1789] [BZOJ1830] [Ahoi2008] Necklace Y-Necklace

Question Description

There are many interesting rides on the island that make the little cocoa fun. Now they're playing a game of string necklaces, and the fastest way to get a good prize. This is not an ordinary necklace, but a Y-shaped necklace, the necklace of the center of a large pearl as a combination of points, from the big pearls connected out of 3 of various gems strung up chain. The rules of the game are this: each time you can remove a gem from one of the three chains, or attach a gem, called an operation, after several operations, and eventually make the three chains exactly the same. To win the game, you can only use as few operations as possible. Let's say that each gem has countless more to use and the chain is long enough. Can you help the few to win the game? Note: There is no special requirement for the number of jewels on the Y-necklace, so even if you take all the gems down, it is an acceptable option (three strings without a string of gems are exactly the same)

Input

A total of 3 lines, representing the Y-type necklace of three chains, each line starts with a number n, indicating that the chain is initially a string of N gems (N<=50), followed by a space, then n ' a ' and ' Z ' characters, representing the gem on this chain, each letter represents a different gem, The leftmost character of the string represents the gem closest to the Big Pearl, and the rightmost one represents the jewel at the end of the chain.

Output

There is only one integer that represents the minimum number of operations required.

Input example

3 CAT 3 TAC 5 CATCH

Output example

8

Data size and conventions

See " Input "

Exercises

Be greedy. Establish the Trie tree, if there is a node has three sons directly cut off three fork, if two nodes have two sons, then the depth of the node two fork is cut off, the depth of the small to the "next" (oneself understand) out of the fork cut off and then a paragraph ... That's about it. It's not hard to prove it, just a little bit.

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring>using namespace std; #define MAXN 200#define maxs 29int ToT = 1, ch[maxn][maxs], CNB[MAXN], DEP[MAXN], SIZ[MAXN], ps[3], Cnt;int idx (char c) { Return C-' A '; }int Clo, L[MAXN], r[maxn];void build (int u) {siz[u] = 1; L[u] = ++clo;for (int i = 0; i < maxs; i++) if (Ch[u][i]) build (Ch[u][i]), siz[u] + = siz[ch[u][i]]; R[u] = ++clo;return;} int main () {char TC = GetChar (), while (!isalpha (TC)) TC = GetChar (), int u = 1, X;while (Isalpha (TC)) {x = IDX (TC); if (!ch[u][ X]) Ch[u][x] = ++tot, cnb[u]++, dep[ch[u][x]] = Dep[u] + 1;u = CH[U][X];TC = GetChar ();} x = 26;if (!ch[u][x]) ch[u][x] = ++tot, cnb[u]++, dep[ch[u][x]] = Dep[u] + 1;while (!isalpha (TC)) TC = GetChar (); u = 1;while (Isalpha (TC)) {x = IDX (TC), if (!ch[u][x]) {ch[u][x] = ++tot; cnb[u]++; dep[ch[u][x]] = Dep[u] + 1;if (cnb[u] = = 2) ps[++cnt] = u;} u = CH[U][X];TC = GetChar ();} x = 27;if (!ch[u][x]) {ch[u][x] = ++tot; cnb[u]++; dep[ch[u][x]] = Dep[u] + 1;if(Cnb[u] = = 2) ps[++cnt] = u;} while (!isalpha (TC)) TC = GetChar (), u = 1;while (Isalpha (TC)) {x = IDX (TC), if (!ch[u][x]) {ch[u][x] = ++tot; cnb[u]++; dep[ch [U] [x]] = Dep[u] + 1;if (cnb[u] = = 2) ps[++cnt] = u;} u = CH[U][X];TC = GetChar ();} x = 28;if (!ch[u][x]) {ch[u][x] = ++tot; cnb[u]++; dep[ch[u][x]] = Dep[u] + 1;if (cnb[u] = 2) ps[++cnt] = u;} Build (1), if (cnt = = 1) printf ("%d\n", Siz[ps[1]]-4), else {int a = Ps[1], B = ps[2], ans = 0;if (Dep[a] < dep[b]) swap (a , b); for (int i = 0; i < maxs; i++) if (Ch[b][i] && (L[ch[b][i]] > R[a] | | L[a] > R[ch[b][i]]) {ans + siz[ch[b][i]] + dep[a]-dep[b]; break;} Ans + = Siz[a]-1;printf ("%d\n", ans-3);} return 0;} /*32 ABNXKSKDNSKGJSLKJSHSDJKHGJSXDJFS21 ABNXKJSLKJSHSDJKHGJSX32 ABNXKSKDNSKGJSLKJSHSDJKHGJSXDJFS5 ABCBA4 ABDB5 ABCBA */

[BZOJ1789] [BZOJ1830] [Ahoi2008] Necklace Y-Necklace

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.