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. iOS数据持久化--归档

    一.简介 在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦: 偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置信息 归档:因为 ...

  2. salt-api return mysql返回的使用,记录操作日志

    说在前面 折腾这个搞了半天,现做下记录 安装依赖(操作只在master端) yum install mysql-python or pip install mysql-python master端本地 ...

  3. centOS7.10 KDE桌面字体设置推荐

    安装完centOS7.10的KDE桌面后,第一次使用觉得字体太难看了,特别是终端,看着很难受,调整多次后觉得如下设置舒服很多,分享出来以供参考. 其中等宽字 这样整体看着就会舒服很多 ******** ...

  4. Docker端口映射(六)

    一.容器端口映射 1.1. 外部访问容器 在启动容器时候,如果不指定参数,在容器外部是无法通过网络来访问容器内的服务的 当容器运行一些网络服务的时候,我们可以通过指定-p或者-P参数来实现能够让外部访 ...

  5. 10 Tips for Optimizing Your Website’s Speed

    转自:http://sixrevisions.com/web-development/site-speed-performance/ Web page speed and performance is ...

  6. MVC3学习:Sql Server2005中时间类型DateTime的显示

    在Sql Server2005中,如果将某字段定义成日期时间类型DateTime,那么在视图中会默认显示成年月日时分秒的方式(如 2013/8/6 13:37:33) 如果只想显示成年月日形式,不要时 ...

  7. Python:使用异常处理来判断运行的平台

    try: import termios, TERMIOS 1 except ImportError: try: import msvcrt 2 except ImportError: try: fro ...

  8. Identity Server4学习系列三

    1.简介 在Identity Server4学习系列一和Identity Server4学习系列二之令牌(Token)的概念的基础上,了解了Identity Server4的由来,以及令牌的相关知识, ...

  9. js字符串替换

    <script language="javascript">var r= "1\n2\n3\n";//将字母\n替换成分号alert(r.repla ...

  10. 高可用Hadoop平台-集成Hive HAProxy

    1.概述 这篇博客是接着<高可用Hadoop平台>系列讲,本篇博客是为后面用 Hive 来做数据统计做准备的,介绍如何在 Hadoop HA 平台下集成高可用的 Hive 工具,下面我打算 ...