首页 > 其他 > 详细

usaco题目分享——Longest Prefix

时间:2016-01-24 11:31:03      阅读:157      评论:0      收藏:0      [点我收藏+]
Longest Prefix
IOI‘96

The structure of some biological objects is represented by the sequence of their constituents, where each part is denoted by an uppercase letter. Biologists are interested in decomposing a long sequence into shorter sequences called primitives.

We say that a sequence S can be composed from a given set of primitives P if there is a some sequence of (possibly repeated) primitives from the set whose concatenation equals S. Not necessarily all primitives need be present. For instance the sequence ABABACABAABcan be composed from the set of primitives

	   {A, AB, BA, CA, BBC}

The first K characters of S are the prefix of S with length K. Write a program which accepts as input a set of primitives and a sequence of constituents and then computes the length of the longest prefix that can be composed from primitives.

PROGRAM NAME: prefix

INPUT FORMAT

First, the input file contains the list (length 1..200) of primitives (length 1..10) expressed as a series of space-separated strings of upper-case characters on one or more lines. The list of primitives is terminated by a line that contains nothing more than a period (`.‘). No primitive appears twice in the list. Then, the input file contains a sequence S (length 1..200,000) expressed as one or more lines, none of which exceeds 76 letters in length. The "newlines" (line terminators) are not part of the string S.

SAMPLE INPUT (file prefix.in)

A AB BA CA BBC
.
ABABACABAABC

OUTPUT FORMAT

A single line containing an integer that is the length of the longest prefix that can be composed from the set P.

SAMPLE OUTPUT (file prefix.out)

11

很长知识的一道题目。对于字符的运用的考察。
可以参考代码,也可以自己尝试用其他方法。
当然,思维是一种基于模板的比较,来判断是否满足题意。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int len[205],o,lent,ans;
char f[205][12],r[205];
string s="",hh;
int main()
{
    freopen("prefix.in","r",stdin);
    freopen("prefix.out","w",stdout);
    int i,j,k;
    o=1;
    scanf("%s",f[o]);
    len[o]=strlen(f[o]);
    while(f[o][0]!=.)
    {
        o++;
        scanf("%s",f[o]);
        len[o]=strlen(f[o]);
    }
    o--;
    while(scanf("%s",r)==1)
        s+=r;
    lent=s.length();
    for(i=0;i<lent;++i)
    {
        for(j=1;j<=o;++j)
        {
            if(i+len[j]>lent) continue;
            int is=0;
            for(k=0;k<len[j];++k)
                if(s[i+k]!=f[j][k])
                {
                    is=1;
                    break;
                }
            if(is==1) continue;
            ans=max(ans,i+len[j]);
        }
        if(i+1>ans) break;
    }
    printf("%d\n",ans);
    return 0;
}

 

usaco题目分享——Longest Prefix

原文:http://www.cnblogs.com/hhhhhhhhhh/p/5154802.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!