zoukankan      html  css  js  c++  java
  • 51nod 1732 LCS变形

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1732

    1732 51nod婚姻介绍所

    题目来源: 原创
    基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
    收藏
    关注
    51nod除了在做OJ之外,还开展了很多副业。婚姻介绍所就是其中之一。
     
    对于一个客户,我们可以使用一个字符串来描述该客户的特质。
     
    假设现在我们有两个客户A和B。
     
    A的特质字符串为:abcdefg
    B的特质字符串为:abcxyz
     
    则A和B的匹配度f(A, B)为A和B的最长公共前缀的长度,即len('abc') = 3
     
    由于最近51nod经费紧张,所以夹克大老爷设计了一种压缩算法以节约内存。
     
    所有用户的特质字符串都被存储在了一个长为n的字符串S中。(n <= 1000)用户的特质使用一个整数p表示,表示该用户的特质字符串为S[p...n - 1]。
     
    现给定字符串S,与q次查询<ai, bi>(ai, bi分别为合法的用户特质整数)。请输出q次查询分别对应的客户匹配度。
     
     
     
    Input
    现给定字符串长度n,与字符串S。接下来是整数q,代表接下来有q次查询。
    下面q行有两个整数ai, bi。代表查询特质为ai与bi的用户的匹配度。
    
    1 <= n <= 1000
    1 <= q <= 10^6
    
    输入数据全部合法。
    Output
    每一行输出一个用户匹配度整数。
    Input示例
    12
    loveornolove
    5
    3 7
    0 0
    9 1
    3 1
    9 5
    Output示例
    0
    12
    3
    0
    0
    f[i][j]表示从i开始和从j开始的字符串的最长公共前缀,显然 f[i][j]={ f[i+1][j+1]+1|if(s[i]==s[j] , 0 }
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define LL long long
     4 LL mod=1e9+7;
     5 char s[1005];
     6 int f[1005][1005];
     7 int main()
     8 {
     9     int len,n,q,i,j,k;
    10     while(scanf("%d",&len)==1){
    11         int a,b;
    12         scanf("%s",s+1);
    13         scanf("%d",&q);
    14         memset(f,0,sizeof(f));
    15         for(i=len;i>=1;--i)
    16             {
    17                 for(j=len;j>=1;--j)
    18                 {
    19                     if(s[i]==s[j]) f[i][j]=f[i+1][j+1]+1;
    20                     else f[i][j]=0;
    21                 }
    22             }
    23         while(q--){
    24             scanf("%d%d",&a,&b);
    25             a++;
    26             b++;
    27             printf("%d
    ",f[a][b]);
    28         }
    29     }
    30     return 0;
    31 }
    
    
  • 相关阅读:
    使用 libevent 和 libev 提高网络应用性能
    An existing connection was forcibly closed by the remote host
    各种浏览器的兼容css
    vs输出窗口,显示build的时间
    sass
    网站设置404错误页
    List of content management systems
    css footer not displaying at the bottom of the page
    强制刷新css
    sp_executesql invalid object name
  • 原文地址:https://www.cnblogs.com/zzqc/p/7467559.html
Copyright © 2011-2022 走看看