USACO-Longest Prefix

本文介绍了一种算法,该算法接收一组生物序列的原始片段集合及一个长序列,通过匹配来找出能由原始片段集合构成的最长前缀。文章详细解释了输入格式,并提供了一个C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:给一个集合和一串字母,问这串字母中由集合中子集构成的最大前缀和。

一步步的往后走就可以了。

代码:

/*
ID:iam666
PROG:prefix
LANG:C++
*/
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int tmp;
char a[210][20];
string str; 
int main (void)
{
freopen("prefix.in","r",stdin);
freopen("prefix.out","w",stdout);
//printf("helaaaalo\n");
int cnt=0,Max=0;
while(cin>>a[cnt]&&a[cnt++][0]!='.');
str="";
string s;
while(cin>>s)
str+=s;
for(int i=0;i<str.length();i++)
{
    for(int j=0;j<cnt;j++)
    {
        if(i+strlen(a[j])<=str.length());
        {
            bool jud=true;
            for(int k=0;k<strlen(a[j]);k++)
            {
                if(str[i+k]!=a[j][k])
                {
                    jud=false;
                    break;
                }
            }
            if(jud)
            {
                if(Max<i+strlen(a[j]))
                Max=i+strlen(a[j]);
            }
        }
    }
    if(i+1>Max)
    break;
}
   printf("%d\n",Max);
    return 0;
}
### USACO 1327 Problem Explanation USACO 1327涉及的是一个贪心算法中的区间覆盖问题。具体来说,这个问题描述了一组奶牛可以工作的班次范围,并要求找出最少数量的奶牛来完全覆盖所有的班次。 对于此类问题的一个有效方法是采用贪心策略[^1]。首先按照区间的结束时间从小到大排序这些工作时间段;如果结束时间相同,则按开始时间从早到晚排列。接着遍历这个有序列表,在每一步都尽可能选择最早能完成当前未被覆盖部分的工作时段。通过这种方式逐步构建最终解集直到所有的时间段都被覆盖为止。 为了提高效率并防止超时错误,建议使用`scanf()`函数代替标准输入流操作符`cin`来进行数据读取处理[^2]。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Interval { int start; int end; }; bool compareIntervals(const Interval& i1, const Interval& i2) { return (i1.end < i2.end || (i1.end == i2.end && i1.start < i2.start)); } int main() { vector<Interval> intervals = {{1, 7}, {3, 6}, {6, 10}}; sort(intervals.begin(), intervals.end(), compareIntervals); int currentEnd = 0; int count = 0; for (const auto& interval : intervals) { if (interval.start > currentEnd) break; while (!intervals.empty() && intervals.front().start <= currentEnd) { if (intervals.front().end >= interval.end) { interval = intervals.front(); } intervals.erase(intervals.begin()); } currentEnd = interval.end; ++count; if (currentEnd >= 10) break; // Assuming total shift length is known. } cout << "Minimum number of cows needed: " << count << endl; } ``` 此代码片段展示了如何实现上述提到的方法解决该类问题。需要注意的是实际比赛中可能还需要考虑更多边界条件以及优化细节以满足严格的性能需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值