Uva-10635-prince and Princess (LCS converted to LIS)

Source: Internet
Author: User


Topic Transfer: UVA-10635


Idea: The direct idea is two strings of LCS, but this problem can be converted to LIS, because said the sequence of elements are different, so you can map out the second character in the string corresponding to the position of the characters in the first string (does not exist is deleted), and then calculate the location of the LIS can


AC Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set > #include <deque> #include <cctype> #define LL long long#define INF 0x7fffffffusing namespace Std;const int MAXN = [+] + 10;int S[MAXN], G[maxn];int num[maxn];//records the position of each digit in the first array int N, p, q;int main () {int t;int cas = 1;sca  NF ("%d", &t), while (T--) {scanf ("%d%d%d", &n, &p, &q), memset (num, 0, sizeof (NUM)); for (int i = 1; I <= p + 1; i + +) {int t;scanf ("%d", &t); num[t] = i;} int n = 0;for (int i = 1; I <= q + 1; i + +) {int t;scanf ("%d", &t), if (num[t]! = 0) {s[n + +] = Num[t];}} N*logn for an S array of lis for (int i = 1; I <= n; i + +) g[i] = inf;int ans = 0;for (int i = 0; i < n; i + +) {int pos = Lower_bo und (g + 1, G + N + 1, s[i])-G;//n*logn find G[pos] = S[i];ans = max (ans, POS); }printf ("Case%d:%d\n", CAs + +, ans);} return 0;} 












Uva-10635-prince and Princess (LCS converted to LIS)

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.