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. AreaHttpControllerSelector 对 Web Api 实现 Area 路由控制

    结合此文章:http://www.cnblogs.com/wuhuacong/p/5828038.html using System; using System.Collections.Concurr ...

  2. docker部署PiggyMetrics分布式微服务

    在上一篇文章里http://www.cnblogs.com/lyhero11/p/8686058.html, 讲解了如何在windows10下安装docker社区版. 那如何利用docker落地一个分 ...

  3. ubuntu 环境 openstack 源码包制成 deb 包

    安装软件: sudo apt-get install dh-make checkinstall cd neutron sudo checkinstall -D -y -install=no -pkgv ...

  4. underscore.js源码研究(4)

    概述 很早就想研究underscore源码了,虽然underscore.js这个库有些过时了,但是我还是想学习一下库的架构,函数式编程以及常用方法的编写这些方面的内容,又恰好没什么其它要研究的了,所以 ...

  5. for,while陈述

    今天我们来说一下for 和while循环 Python循环语句的控制结构图如下所示: for 是Python程序员使用最多的语句,for 循环用于迭代容器对象中的元素,这些对象可以是列表.元组.字典. ...

  6. eslint 配置及规则说明

    中文官方网站 安装 可以全局安装,也可以在项目下面安装. 如下是在项目中安装示例,只需要在 package.json 中添加如下配置,并进行安装: “eslint”: “^4.11.0” 配置 配置方 ...

  7. Git - 信息查看

    git help git version # Display the version of git. git help # Prints the synopsis and a list of the ...

  8. 整理的开源项目(全c#)

    NPOI:读写office办公软件(不需要安装office软件) http://npoi.codeplex.com/downloads/get/70099 消息中间件:DotNetMQ http:// ...

  9. ubuntu升级pip后, ImportError: cannot import name ‘main‘

    场景描述: 原先pip安装完成之后,一直没有更新版本,原pip版本为8.1.1,今天安装python 包pysftp的时候,提示需要升级pip到(pip 10.0.1); 于是乎,直接手到擒来,终端命 ...

  10. java—单例设计模式

    单例设计模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 1.构造方法私有化 2.声明一个本类对象 3.给外部提供一个静态方法获取对象实例 什么时候使用? 1.通过在工具类的设计中使用: ...