zoukankan      html  css  js  c++  java
  • POJ 2217 LCS(后缀数组)

    Secretary
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 1655   Accepted: 671

    Description

    The basic condition of success of a political party, it is the good Election Programme. PSOS know about it, so they entrust the top secretary Juliet with this task. Because she wanted to make her work easier, she used her charm to talk round her friend Romeo to help her. Romeo is assistent of another political party and he was writing the programme some time ago. While writing the Programme for Juliet, he used some parts of his previous programme. When he gave the finished Programme to Juliet, they recognized that both programmes are too similar and that someone could notice it. They need to determine the longest part of text which is common to both programmes.

    Input

    At the first line there is a positive integer N stating the number of assignments to follow. Each assignment consists of exactly two lines of text, each of them contains at most 10000 characters. The end-of-line character is not considered to be a part of the text.

    Output

    Print a single line of text for each assignment. The line should contain the sentence "Nejdelsi spolecny retezec ma delku X." (The longest common part of text has X characters). Replace X with the length of the longest common substring of both texts.

    Sample Input

    2
    Tady nejsou zadni mimozemstani.
    Lide tady take nejsou.
    Ja do lesa nepojedu.
    V sobotu pojedeme na vylet.

    Sample Output

    Nejdelsi spolecny retezec ma delku 7.
    Nejdelsi spolecny retezec ma delku 5.
    

    Source

    题意:求LCS长度。
    代码:
     1 //#include"bits/stdc++.h"
     2 #include"cstdio"
     3 #include"map"
     4 #include"set"
     5 #include"cmath"
     6 #include"queue"
     7 #include"vector"
     8 #include"string"
     9 #include"cstring"
    10 #include"ctime"
    11 #include"iostream"
    12 #include"cstdlib"
    13 #include"algorithm"
    14 #define db double
    15 #define ll long long
    16 #define ull unsigned long long
    17 #define vec vector<ll>
    18 #define mt  vector<vec>
    19 #define ci(x) scanf("%d",&x)
    20 #define cd(x) scanf("%lf",&x)
    21 #define cl(x) scanf("%lld",&x)
    22 #define pi(x) printf("%d
    ",x)
    23 #define pd(x) printf("%f
    ",x)
    24 #define pl(x) printf("%lld
    ",x)
    25 //#define rep(i, x, y) for(int i=x;i<=y;i++)
    26 #define rep(i, n) for(int i=0;i<n;i++)
    27 using namespace std;
    28 const int N   = 1e6 + 5;
    29 const int mod = 1e9 + 7;
    30 const int MOD = mod - 1;
    31 const int inf = 0x3f3f3f3f;
    32 const db  PI  = acos(-1.0);
    33 const db  eps = 1e-10;
    34 int sa[N];
    35 int rk[N];
    36 int tmp[N];
    37 int lcp[N];
    38 int n,k,T;
    39 bool cmp(int i,int j){
    40     if(rk[i] != rk[j]) return rk[i]<rk[j];
    41     else
    42     {
    43         int ri=i+k<=n?rk[i+k]:-1;
    44         int rj=j+k<=n?rk[j+k]:-1;
    45         return ri<rj;
    46     }
    47 }
    48 void bulid(string s,int *sa)
    49 {
    50     n=(int)s.size();
    51     for(int i=0;i<=n;i++){
    52         sa[i]=i;
    53         rk[i]=i<n?s[i]:-1;
    54     }
    55     for(k=1;k<=n;k*=2){
    56         sort(sa,sa+n+1,cmp);
    57         tmp[sa[0]]=0;
    58         for(int i=1;i<=n;i++){
    59             tmp[sa[i]]=tmp[sa[i-1]]+(cmp(sa[i-1],sa[i])?1:0);
    60         }
    61         for(int i=0;i<=n;i++){
    62             rk[i]=tmp[i];
    63         }
    64     }
    65 }
    66 void LCP(string s,int *sa,int *lcp){
    67     n=(int)s.size();
    68     for(int i=0;i<=n;i++) rk[sa[i]]=i;
    69     int h=0;
    70     lcp[0]=0;
    71     for(int i=0;i<n;i++){
    72         int j=sa[rk[i]-1];
    73         for (h ? h-- : 0; j + h < n&&i + h < n&&s[j + h] == s[i + h]; h++);
    74         lcp[rk[i]-1] = h;
    75     }
    76 }
    77 int main()
    78 {
    79     ios::sync_with_stdio(0);
    80     cin>>T;
    81     getchar();
    82     while(T--)
    83     {
    84         string s,t;
    85         getline(cin,s);//read a line 
    86         getline(cin,t);
    87         int s1=(int)s.size();
    88         s+='$'+t;
    89         n=(int)s.size();
    90         bulid(s,sa);
    91         LCP(s,sa,lcp);
    92         int ma=0;
    93         for(int i = 0;i < n; i++){
    94             if((sa[i]<s1)!=(sa[i+1]<s1)) ma=max(ma,lcp[i]);
    95         }
    96         printf("Nejdelsi spolecny retezec ma delku %d.
    ",ma);
    97     }
    98     return 0;
    99 }
  • 相关阅读:
    C#ModBus Tcp 报文解析
    C#ModBus Tcp的学习及Master的实现
    C#原生Socket服务器与客户端的实现
    转载:C# 创建、部署和调用WebService的简单示例
    记kepServer读写西门子PLC
    C#实现某一属性值变化时触发事件
    C#倒计时关闭提示框
    C#SuperSocket的搭建--通过配置启动
    C#串口扫描枪的简单实现
    C#SuperSocket服务器的简易实现
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/9020160.html
Copyright © 2011-2022 走看看