zoukankan      html  css  js  c++  java
  • UVA_10653 公主与王子 #刘汝佳DP题刷完计划

    题意如蓝书66页例题27所示。

    这个问题描述了一个LCS的特殊情况——单个字符串内所有元素各不相同。

    题目要求输入两个数字串,A,B,要求求出最长公共字串。且数字上限是256*256。

    做法:数组A表示为256*256的大数组,每一位表示标号元素的出现位置

    数组B表示为“数组A中有的每一位元素的出现位置”,如果有公共串,就必然满足在串B编号递增。复杂度从O(N^2)升级到O(nlogn)

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const long long MAXN=256*256+233;
     4 long long stqa[MAXN];
     5 long long stqb[MAXN];
     6 long long t,n,p,q;
     7 long long privq[MAXN];
     8 int point =0;
     9 int main()
    10 {
    11     cin.sync_with_stdio(false);
    12     cin>>t;
    13     for(int itee=0;itee<t;++itee)
    14     {
    15         memset(stqa,0,sizeof(stqa));
    16         memset(stqb,0,sizeof(stqb));
    17         memset(privq,0,sizeof(privq));
    18         point=0;
    19         cin>>n>>p>>q;
    20         for(int i=1;i<=p+1;++i)
    21         {
    22             int num;cin>>num;
    23             stqa[num]=i;
    24         }
    25         for(int i=1;i<=q+1;++i)
    26         {
    27             int num;cin>>num;
    28             stqb[i]=stqa[num];
    29         }
    30         for(int i=1;i<=q+1;++i)
    31         {
    32             if(stqb[i]==0)continue;
    33             int pos=lower_bound(privq,privq+point,stqb[i])-privq;
    34             if(pos==point)
    35             {
    36                 point++;
    37             }privq[pos]=stqb[i];            
    38         }
    39         cout<<"Case "<<itee+1<<": "<<point<<endl;
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    二项分布和多项分布
    TF-IDF学习笔记(二)
    TF-IDF学习笔记(一)
    jieba中文分词
    爬虫利器3:Xpath语法与lxml库
    Python爬虫利器1:Requests库的用法
    Python爬虫实战一之爬取糗事百科段子
    Python中的逻辑运算符
    Python:闭包函数与装饰器
    Python:函数名称空间与作用域:
  • 原文地址:https://www.cnblogs.com/rikka/p/7295952.html
Copyright © 2011-2022 走看看