Substrings
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11006 | Accepted: 3827 |
Description
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.
Input
The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.
Output
There should be one line per test case containing the length of the largest string found.
Sample Input
2 3 ABCD BCDFF BRCD 2 rose orchid
Sample Output
2 2
Source
题意:
给出T(1~10)个case,每个case都给出一个N(1~100),后给出N个字符串,求出最长的公共子串,这个子串可以是原序或者逆序存在。
思路:
KMP。同样也是列举第一个串,注意理解题目中的“ 原序或者逆序其中之一 ”是什么意思。子串可以以原序或者逆序存在于剩下的N - 1个字符串中,说明在N - 1个字符串中可能含有逆序的子串,也可能存在原序的子串。而不是N - 1个字符串全部只包含原序或者逆序子串的其中之一。
AC:
#include<stdio.h> #include<string.h> char str[105][105]; int n,maxnum; void turn(int len,char *inve,char *orig) { for(int i = 0;i < len;i++) inve[i] = orig[len - i - 1]; } void get_next(int len,int *next,char *orig) { int i = 0,j = -1; next[0] = -1; while(i < len) { if(j == -1 || orig[i] == orig[j]) next[++i] = ++j; else j = next[j]; } } int kmp(char *orig,char *match,int *next) { int len1 = strlen(match),len2 = strlen(orig); int i = 0,j = 0; while(i < len1 && j < len2) { if(j == -1 || match[i] == orig[j]) { i++; j++; } else j = next[j]; } if(j == len2) return 1; return 0; } int main() { int c,next[105],next1[105]; char p[105],p1[105]; scanf("%d",&c); while(c--) { scanf("%d",&n); maxnum = 0; for(int i = 1;i <= n;i++) scanf("%s",str + i); for(int i = 0;i < strlen(str[1]);i++) for(int j = 1;j <= strlen(str[1]) - i;j++) { int k; memset(p,0,sizeof(p)); memset(p1,0,sizeof(p1)); memset(next,0,sizeof(next)); memset(next1,0,sizeof(next1)); strncpy(p,str[1] + i,j); turn(j,p1,p); get_next(j,next,p); get_next(j,next1,p1); for(k = 2;k <= n;k++) if(!kmp(p,str[k],next) && !kmp(p1,str[k],next1)) break; if(k == n + 1 && j > maxnum) maxnum = j; } printf("%d\n",maxnum); } return 0; }
相关推荐
试题 算法提高 Substrings 问题描述 You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring...
Matching and extraction of matches / substrings from the source text. Searching for regular expressions within streams and memory buffers. To search within streams or files (of virtually unlimited ...
Matching and extraction of matches / substrings from the source text. Searching for regular expressions within streams and memory buffers. To search within streams or files (of virtually unlimited ...
Matching and extraction of matches / substrings from the source text. Searching for regular expressions within streams and memory buffers. To search within streams or files (of virtually unlimited ...
线性子串给定任何字符串S,该算法将在O(n)时间内确定S的字典顺序上的最后一个子字符串,其中n = S的长度。 附加的功能: -给定任何长度L,以使0 <= L <= n,该算法将在O(n)时间内按字典顺序确定S的长度L的...
Java Program To Find All Substrings Of A String
What You Will Learn Formulate an expression Work with arbitrary char classes, ... Handle substring extraction from regex using Perl 6 capture groups, capture substrings, and reuse substrings
substrings (i.e. left substring and right substring). The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring. Example 1: Input: ...
Suffix trees have numerous applications, often providing linear-time solutions to challenging string ...Common substrings, with applications Matching statistics Suffix arrays Genome-scale projects
Substrings * Problem URL : https://leetcode.com/problems/palindromic-substrings/ * Date : Oct 17 2017 * Complexity : O(n^3) Time, O(n) space * Author : Atiq Rahman * Status : Accepted * Notes : Track ...
文章目录前言一般情况限制切割次数结果出现”pattern使用括号pattern使用多重括号patter同时用 () 和 | 会出现...returning a list containing the resulting substrings. If capturing parentheses are used in patter
For instance, size filters (width, height), URL filters (include, exclude substrings), address name filter (name wildcard) and others. With GetWebPics you can download various types of pictures, ...
去食谱 来源,一个社区为现实世界的 Golang 开发构建并贡献了实用食谱的集合,由支持。 贡献 Go Cookbook 由支持,但由社区构建,因此非常... - title: Detecting All Substrings path: false 指定wip: true将“[正在
================ LeetCode ================动态编程1.... Palindromic Substrings [647]9. Maximum Length of Pair Chain [646]10. Integer Break [343]11. Count Numbers with Unique Digits [357]12. 2-Key
分组, , , , , , , , , , , , ,前瞻和回顾间谍, 偷看, 可寻加窗windowed , 子字符串, substrings_indexes , 交错, windowed_complete , 成对增广count_cycle , 散布, 填充, mark_ends , ...
解决问题 解决数据结构和算法问题 ...Sam_and_substrings 第18天 培养细菌 第19天 费多和新游戏 第20天 夏季抛售 第21天 最小三进制字符串 第22天 塔楼 第24天 DZY热爱物理 第25天 几何级数 第26天 功率总和
-02.04 连续字符串02.05 计算表达式-02.05 平均Step-02.05 一位数字-02.05 https://www.codewars.com/kata/单位数非偶数子字符串02.06 https://www.codewars.com/kata/non-even-substrings 未知数量的重复项。...
金鱼草 易于使用的插件系统,用于创建功能强大,快速且通用的解析器和编译器,并具有内置的源映射支持。 请考虑关注该项目的作者 ,... // used for parsing substrings to create tokens . set ( 'foo' , function
List Slicing and Substrings x elif ("Else If") Statements x And that's it! x Dictionaries x Sets of Words for Hangman x Chapter 6 - Tic Tac Toe x Source Code x Designing the Program x Game AI x ...
create and use strings and substrings, and see the various common operations available for strings. Lesson 6, Functional Programming and Lazy Operations, ventures at functional programming and ...