Substrings(hdu1238)字符串匹配
Substrings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7205 Accepted Submission(s):
3255
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.
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.
length of the largest string found.
这个找字串的问题,题目大概意思就是找出所有字符串中共同拥有的一个子串,
该子串(正、逆字符)是任何一个母串的子串,求该子串的最长长度。
想到用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)字符串匹配的更多相关文章
- bitset 的妙用:乱搞字符串匹配
最近碰到了几次 bitset 乱搞字符串匹配的情况,故写文以记之. 1. 算法简介 核心思想:假设文本串为 \(s\),则对字符集中的每一个字符 \(c\) 开一个大小为 \(|s|\) 的 bits ...
- 字符串匹配的KMP算法
~~~摘录 来源:阮一峰~~~ 字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串”BBC ABCDAB ABCDABCDABDE”,我想知道,里面是否包含另一个字符串”ABCDABD”? 许 ...
- {Reship}{KMP字符串匹配}
关于KMP字符串匹配的介绍和归纳,作者的思路非常清晰,推荐看一下 http://blog.csdn.net/v_july_v/article/details/7041827
- 字符串匹配(hash算法)
hash函数对大家来说不陌生吧 ? 而这次我们就用hash函数来实现字符串匹配. 首先我们会想一下二进制数. 对于任意一个二进制数,我们将它化为10进制的数的方法如下(以二进制数1101101为例): ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- sdut 2125串结构练习--字符串匹配【两种KMP算法】
串结构练习——字符串匹配 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目链接:http://acm.sdut.edu.cn/sduto ...
- C语言字符串匹配函数
C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...
- 字符串匹配--Karp-Rabin算法
主要特征 1.使用hash函数 2.预处理阶段时间复杂度O(m),常量空间 3.查找阶段时间复杂度O(mn) 4.期望运行时间:O(n+m) 本文地址:http://www.cnblogs.com/a ...
- 字符串匹配的KMP算法详解及C#实现
字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...
- zstu.4194: 字符串匹配(kmp入门题&& 心得)
4194: 字符串匹配 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 206 Solved: 78 Description 给你两个字符串A,B,请 ...
随机推荐
- 【BZOJ3280】 小R的烦恼(费用流,建模)
有很浓厚的熟悉感?餐巾计划问题? 不就是多了几个医院,相当于快洗部和慢洗部开了分店. 考虑建图: 如果把每一天拆成两个点,一个表示需求,另一个表示拥有的话. 显然就是一个两边的图,考虑如果我现在有人, ...
- 使用Squid部署代理服务
Squid是Linux系统中最为流行的一款高性能代理服务软件,通常用作Web网站的前置缓存服务,能够代替用户向网站服务器请求页面数据并进行缓存.简单来说,Squid服务程序会按照收到的用户请求向网站源 ...
- PHP 获取两个时间之间的月份
## 获取两个时间之间的间距时间 $s = '2017-02-05'; $e = '2017-07-20'; $start = new \DateTime($s); $end = new \DateT ...
- ubuntu升级pip后, ImportError: cannot import name ‘main‘
场景描述: 原先pip安装完成之后,一直没有更新版本,原pip版本为8.1.1,今天安装python 包pysftp的时候,提示需要升级pip到(pip 10.0.1); 于是乎,直接手到擒来,终端命 ...
- 理解WSGI
WSGI是什么? WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义 ...
- (转)详解Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数
原文:https://www.jb51.net/article/138363.htm hasattr()函数 hasattr()函数用于判断是否包含对应的属性 语法: hasattr(object,n ...
- Android学习总结——输入法将BottomNavigationBar(底部导航栏)顶上去的问题
在应用清单中给当前<Activity>设置: android:windowSoftInputMode="adjustPan" 关于android:windowSoftI ...
- Linux 数据重定向
名称 描述 代码 表示 stdin 标准输入 0 < 或 << stdout 标准输出 1 > 或 >> stderr 标准错误输出 2 2> 或 2> ...
- 配置Zookeper
在整个的codis架构之中,codis-proxy将作为所有的codis-server的对外的代理,而在整个的处理之中,codis-proxy所保存的数据一定是所有的codis-server的信息,那 ...
- 面试题-----按位翻转32位unsigned
// test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include < ...