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
题意:
给出一系列单词(1 ~ 200),每个单词长度 1 ~ 10,直到输入“ . ”表示结束。后给出一个字符串(1 ~ 200000),输出字符串最长的前缀长度,这个前缀能由给出的单词组成。
思路:
字典树存储单词,后在字典树上DFS,同时剪枝。标记单词构成的每段字符串来剪枝,不然会超时。
AC:
/* TASK:prefix LANG:C++ ID:sum-g1 */ #include <string.h> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef struct no { struct no *next[30]; int temp; }node; char aim[200050]; int test[200050]; int max_len,len; node *creat_node() { node *p = new node; for(int i = 0;i < 30;i++) p -> next[i] = NULL; p -> temp = 0; return p; } void insert_str(char *str,node *head) { int str_len = strlen(str); node *p = head; for(int i = 0;i < str_len;i++) { int c = str[i] - 'A'; if(p -> next[c] == NULL) p -> next[c] = creat_node(); p = p -> next[c]; } p -> temp = 1; return; } void dfs(int idex,node *head) { if(idex > max_len) max_len = idex; if(idex == len) return; //若当前下标等于目标串长度则返回 if(test[idex]) return; test[idex] = 1; //test 数组标记状态 int i = idex,k = aim[i] - 'A'; node *p = head; while(p -> next[k] != NULL) { i++; p = p -> next[k]; if(i < len) k = aim[i] - 'A'; //若i == len时也访问数组的话会数组越界 if(p -> temp) dfs(i,head); //凡是单词都搜索一遍,以下一个下标开始 //若是最后一个字母也会进行向下搜索 if(!p -> temp && i == len) return; //当为最后一个字母却不是单词时返回 } return; } int main() { freopen("prefix.in","r",stdin); freopen("prefix.out","w",stdout); char vab[50],str[200]; node *head = creat_node(); max_len = 0; memset(test,0,sizeof(test)); while(~scanf("%s",vab) && vab[0] != '.') { insert_str(vab,head); } while(~scanf("%s",str)) { strcat(aim,str); } len = strlen(aim); dfs(0,head); printf("%d\n",max_len); return 0; }
相关推荐
LeetCode Longest Common Prefix解决方案 该解决方案旨在解决LeetCode平台上的一道编程题目,即Longest Common Prefix(最长公共前缀),该问题要求在一个字符串数组中找到最长的公共前缀字符串。如果没有公共前缀...
c c语言_leetcode 0014_longest_common_prefix.zip
java入门 java_leetcode题解之014_Longest_Common_Prefix
js js_leetcode题解之14-longest-common-prefix.js
c语言入门 C语言_leetcode题解之14-longest-common-prefix.c
简单的最长前缀匹配应用 接受输入文件的简单应用程序,其中包含 IP 地址定义和到 ASN 编号的对应映射。 该文件在应用程序启动时加载并用于构建 trie。 Trie 是用于使用最长前缀匹配算法进行搜索的结构。...
【Longest Common Ancestor (LCA)问题】是图论中的一个重要概念,特别是在树结构的处理中。给定树T中的两个节点u和v,LCA指的是离根节点最远的公共祖先,即同时是u和v祖先的那个节点。解决LCA问题可以转化为求解...
LMS Longest Monotonically Increasing Sequence Algorithm
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Java AC 版本
这是动态规划中,求最长公共子序列(Longest common string)的源代码。自己编写执行。程序简单,有注释。
北大POJ2533-Longest Ordered Subsequence【O(nlogn)】
printf("The length of the longest ordered subsequence is: %d\n", longestOrderedSubsequence(nums, numsSize)); return 0; } ``` 在上面的C程序中,我们首先定义了动态规划数组`dp`,然后通过两层循环来填充...
最长公共子序列(Longest Common Subsequence, LCS)问题是计算机科学领域中一个重要的经典问题,它不仅被广泛应用于文本比较、生物信息学中的基因序列比对等领域,而且也是动态规划算法的一个典型应用场景。...
本压缩包文件“c#-Leetcode面试题解之第14题最长公共前缀.zip”显然是针对LeetCode中的第14题——“最长公共前缀”(Longest Common Prefix)提供的解决方案。在这个问题中,我们需要找到一个字符串数组中的最长公共...
Pku acm 第2533题 Longest Ordered Subsequence 代码,有详细的注释,动态规划
2. The list of remotes is now scanned for longest prefix match first. 3. Support for multipacket TSIG signatures for transfers was added, and incorrectly parsed TSIG key secrets without quotes were...
Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with...
后缀数组的主要用途是进行模式匹配和字符串查询,例如在文本中查找所有出现的子串,找出最长重复子串,或者计算LCP(Longest Common Prefix)数组等。在信息检索、生物信息学等领域有广泛应用。 模板文件通常包含了...
标题“POJ2533-Longest Ordered Subsequence”是指北京大学在线判题系统POJ上的一道编程题目,其核心任务是寻找一个序列中最长的有序子序列。描述中的“解题报告+AC代码”表明这个压缩包包含了对这道问题的解答思路...
数据结构问题:“最长回文子串问题”(Longest Palindromic Substring),详细解法和代码实现; 数据结构问题:“最长回文子串问题”(Longest Palindromic Substring),详细解法和代码实现; 数据结构问题:“最长...