Substrings

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7205 Accepted Submission(s):
3255

Problem 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 file 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
 
 

这个找字串的问题,题目大概意思就是找出所有字符串中共同拥有的一个子串,

该子串(正、逆字符)是任何一个母串的子串,求该子串的最长长度。

想到用STRING里的成员函数和STL的reverse反转函数,

思路:

先找出最短的母串,即该符合要求的子串肯定在这个母串中,即在从长到短

从最短母串中取子串,在子串正反去查看是否符合要求。

说实话今天又学到了一些知识,我表示这C++的很多函数真shi 强大啊。

ps:http://acm.hdu.edu.cn/showproblem.php?pid=1238

#include<iostream>
#include<string>
#include<algorithm>//STL reverse函数的头文件,reverse反转函数,
using namespace std;
int main()
{
int cas,len,sub,maxn;
int n,k,i,j;
string s[];
cin>>cas;
while(cas--)
{
cin>>n;
len=;
sub=;
for(i=; i<n; i++)
{
cin>>s[i];
if(len>s[i].size())//找最小 的母串
{
len=s[i].size();
sub=i;
}
}
maxn=;
for(i=s[sub].size(); i>; i--) //从最小的母串开始从长到短找子串,
{
for(j=; j<s[sub].size()-i+; j++) //长度为i的子串在母串中找
{
string s1,s2;//s1为子串正 ,s2为子串反
s1=s[sub].substr(j,i);//去j开始i长度是字符
s2=s1;
reverse(s2.begin(),s2.end());//反串
for( k=; k<n; k++)
{
if(s[k].find(s1,)==-&&s[k].find(s2,)==-) //当正反子串在母串中都未发现时即跳出
break; }
if(k==n&&maxn<s1.size())
maxn=s1.size(); } }
cout<<maxn<<endl;
}
return ;
}

Substrings(hdu1238)字符串匹配的更多相关文章

  1. bitset 的妙用:乱搞字符串匹配

    最近碰到了几次 bitset 乱搞字符串匹配的情况,故写文以记之. 1. 算法简介 核心思想:假设文本串为 \(s\),则对字符集中的每一个字符 \(c\) 开一个大小为 \(|s|\) 的 bits ...

  2. 字符串匹配的KMP算法

    ~~~摘录 来源:阮一峰~~~ 字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串”BBC ABCDAB ABCDABCDABDE”,我想知道,里面是否包含另一个字符串”ABCDABD”? 许 ...

  3. {Reship}{KMP字符串匹配}

    关于KMP字符串匹配的介绍和归纳,作者的思路非常清晰,推荐看一下 http://blog.csdn.net/v_july_v/article/details/7041827

  4. 字符串匹配(hash算法)

    hash函数对大家来说不陌生吧 ? 而这次我们就用hash函数来实现字符串匹配. 首先我们会想一下二进制数. 对于任意一个二进制数,我们将它化为10进制的数的方法如下(以二进制数1101101为例): ...

  5. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  6. sdut 2125串结构练习--字符串匹配【两种KMP算法】

    串结构练习——字符串匹配 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目链接:http://acm.sdut.edu.cn/sduto ...

  7. C语言字符串匹配函数

    C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...

  8. 字符串匹配--Karp-Rabin算法

    主要特征 1.使用hash函数 2.预处理阶段时间复杂度O(m),常量空间 3.查找阶段时间复杂度O(mn) 4.期望运行时间:O(n+m) 本文地址:http://www.cnblogs.com/a ...

  9. 字符串匹配的KMP算法详解及C#实现

    字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...

  10. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

随机推荐

  1. [leetcode.com]算法题目 - Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  2. Code Chef April Cook-Off 2019题解

    传送门 \(PEWDSVTS\) 我哪根筋不对了要把所有可行的拿出来\(sort\)一下--还有忘开\(long\ long\)真的好难受-- int main(){ // freopen(" ...

  3. Python isinstance 与 instance 的用法

    instance: instance 属于python2的关键字,python2中如果一个类没有继承自object, 那么实例化出来的对象就是instance类型,否则就是class类型. isins ...

  4. Flash 0day漏洞(CVE-2018-4878)复现

    该漏洞影响 Flash Player 当前最新版本28.0.0.137以及之前的所有版本,而Adobe公司计划在当地时间2月5日紧急发布更新来修复此漏洞. 本文作者:i春秋作家——F0rmat 前言 ...

  5. Smart/400开发上手4: 调试Cobol代码 (DEBUG with QBATCH)

    Step1:Compile Cobol source CB TIM07 using *SRCDBG option例如:CB MEMBER(TIM07) OPTION(*SRCDBG) WORKU(TI ...

  6. Salt-API安装配置及使用

    Python3使用saltstack和salt-api 安装python3 1. tar zxvf Python-3.5.1.tgz 2. cd Python-3.5.1 3. ./configure ...

  7. 10-05 Java 内部类概述和讲解

    内部类的概述 /* 内部类概述: 把类定义在其他类的内部,这个类就被称为内部类. 举例:在类A中定义了一个类B,类B就是内部类. 内部的访问特点: A:内部类可以直接访问外部类的成员,包括私有. B: ...

  8. [Umbraco] DocumentType设计指南

    1. 命名规则 1.1. 文档类型(DocumentType)命名规则 图 1. Document Type命名示例 名称(Name)   采用帕斯卡命名法 如:TextPage 别名(Alias)  ...

  9. (转载)elasticsearch 查询(match和term)

    原文地址:https://www.cnblogs.com/yjf512/p/4897294.html elasticsearch 查询(match和term) es中的查询请求有两种方式,一种是简易版 ...

  10. 使用 Redis 共享 Session 会话

    储存模式 1.InProc模式 这是ASP.NET默认的Session管理模式,在应用进程内维护Session. 2.StateServer模式 这是在服务器装了.NET环境后自带的一个StateSe ...