usaco题目分享——Longest Prefix

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;
}
时间: 2024-11-05 10:26:22

usaco题目分享——Longest Prefix的相关文章

USACO Section2.3 Longest Prefix 解题报告 【icedream61】

prefix解题报告------------------------------------------------------------------------------------------------------------------------------------------------[题目] 给你许多子串P和一个长字符串S,请问S的前缀中能用这些子串(可以重复使用子串)组合的最长的一个长度是多少?[数据范围] P的长度在1-10之内,子串个数在1-200之内 S的长度在1

usaco题目分享——Bessie Come Home

Bessie Come HomeKolstad & Burch It's dinner time, and the cows are out in their separate pastures. Farmer John rings the bell so they will start walking to the barn. Your job is to figure out which one cow gets to the barn first (the supplied test da

*usaco题目分享——Contact

ContactIOI'98 The cows have developed a new interest in scanning the universe outside their farm with radiotelescopes. Recently, they noticed a very curious microwave pulsing emission sent right from the centre of the galaxy. They wish to know if the

usaco题目分享——Controlling Companies

Controlling Companies Some companies are partial owners of other companies because they have acquired part of their total shares of stock. For example, Ford owns 12% of Mazda. It is said that a company A controls company B if at least one of the foll

usaco题目分享——Agri-Net

Agri-NetRuss Cox Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed connection for his farm and is

洛谷P1470 最长前缀 Longest Prefix

P1470 最长前缀 Longest Prefix 73通过 236提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 求大神指导,为何错? 题目描述 在生物学中,一些生物的结构是用包含其要素的大写字母序列来表示的.生物学家对于把长的序列分解成较短的序列(即元素)很感兴趣. 如果一个集合 P 中的元素可以通过串联(元素可以重复使用,相当于 Pascal 中的 “+” 运算符)组成一个序列 S ,那么我们认为序列 S 可以分解为 P 中的元素.元素不一定

Implement Trie and find longest prefix string list

1 package leetcode; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 class TrieNode{ 7 Boolean isWord;//true if path till this node represent a string. 8 Integer freq;//numbers of strings share the same prefix 9 Character nodeChar;//chara

Luogu P1470 最长前缀 Longest Prefix

Luogu P1470 最长前缀 Longest Prefix Portal(传送门) 注释 这道题与上一篇博客的题几乎一样 解析 有点麻烦的地方就是字符串的输入 方法一:类dp Code #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define LL long long using namespace std

Leetcode题目:Longest Common Prefix

题目: Write a function to find the longest common prefix string amongst an array of strings. 题目解答: 求一组字符串的最长公共前缀.使用迭代器数组实现.思路异常的简单. 代码如下: class Solution {public:    string longestCommonPrefix(vector<string>& strs) {        string res;        int s