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
 
题目意思:找出所给字符串的最大公共子串,这个公共子串可以翻转存在于母串之中。
解题思路:我在这里直接使用了C++ string 中的一些函数处理的,比使用C的逐个字符的遍历好写一些。
贴一篇关于find函数的说明:https://www.cnblogs.com/wkfvawl/p/9429128.html
#include <iostream>
#include <string>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; int main()
{
int t,n,i,j,k;
int mins;
int pos,flag,flag1;
string s[];
string key;
string tem;
string rev;
cin>>t;
while(t--)
{
cin>>n;
pos=;
flag=;
mins=INF;
for(i=;i<n;i++)
{
cin>>s[i];
if(s[i].length()<mins)
{
mins=s[i].length();
pos=i;
}
}
key=s[pos];
for(i=mins;i>=;i--)//子串长度
{
for(j=;j+i-<mins;j++)
{
tem=key.substr(j,i);//截取以j开头长度为i的子串
rev=string(tem.rbegin(),tem.rend());//反向迭代器,翻转后的子串
flag=;
flag1=;
for(k=;k<n;k++)
{
if(s[k].find(tem)==string::npos&&s[k].find(rev)==string::npos)
{
flag=;
break;
}//没有找到这样一个子串
}
if(flag)//找到了一个子串
{
cout<<i<<endl;
flag1=;
break;
}
}
if(!flag1)
{
break;
}
}
if(flag1)
{
cout<<<<endl;
}
}
return ;
}
 

Substrings (C++ find函数应用)的更多相关文章

  1. hihocoder #1152 Lucky Substrings 【字符串处理问题】strsub()函数+set集合去重

    #1152 : Lucky Substrings时间限制:10000ms单点时限:1000ms内存限制:256MB描述A string s is LUCKY if and only if the nu ...

  2. POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串

    题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS   Memory Limit: 10000K Total ...

  3. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数014,tuple,元组

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数014,tuple,元组 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换:“p ...

  4. Ruby:字符串处理函数

    字符串处理函数1.返回字符串的长度 str.length => integer 2.判断字符串中是否包含另一个串 str.include? other_str => true or fal ...

  5. 《zw版·delphi与halcon系列原创教程》zw版_THOperatorSetX控件函数列表 v11中文增强版

    <zw版·delphi与halcon系列原创教程>zw版_THOperatorSetX控件函数列表v11中文增强版 Halcon虽然庞大,光HALCONXLib_TLB.pas文件,源码就 ...

  6. SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转

    694. Distinct Substrings Problem code: DISUBSTR   Given a string, we need to find the total number o ...

  7. postgres函数

    1.数据修复最先考虑通过db内做修复,实在不行,在考虑外部应用程序通过jdbc修复. 比如一个场景:profile_image_url与enlarge_image_url都是微博用户信息返回的字段. ...

  8. hihocoder 1152 Lucky Substrings

    #1152 : Lucky Substrings 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if and only if t ...

  9. lua string函数

    lua的string函数: 参数中的index从1开始,负数的意义是从后开始往前数,比如-1代表最后一个字母 对于string类型的值,可以使用OO的方式处理,如string.byte(s.i)可以被 ...

随机推荐

  1. November 14th, 2017 Week 46th Tuesday

    Eternity is said not to be an extension of time but an absence of time. 永恒不是时间的无限延伸,而是没有时间. What is ...

  2. October 25th, 2017 Week 43rd Wednesday

    Perseverance is not a long race; it is many short races one after another. 坚持不是一个长跑,她是很多一个接一个的短跑. To ...

  3. python安装教程(Windows系统,python3.7为例)

    1. 在python的官网下载python对应版本:https://www.python.org/downloads/windows/ 64位下载Windows x86-64 executable i ...

  4. Test传送门(更新中)

    一.Codeforces传送门: Avito Code Challenge 2018 题解传送门 Codeforces Round #485 (Div. 2)     题解传送门 二.hihocode ...

  5. loli的搜索测试-我真不知道是第多少次了

    搜索测试 又到了....并不激动人心的搜索测试时间. 今天和以前还是有一点不一样的,新高二的学长们也参加了(也就是说我们又要被吊打了) 话不多说,看题: fz:填一个5*5的质数方阵,要求每行,每列, ...

  6. Jmeter核心-hc课堂笔记

    自动化测试平台-Httprunner-接口.UI.协议. 平台语言-JAVA-UI-Selenium(java版的).Appium(java版的).接口-Httpclient.Jmeter.(Jmet ...

  7. ORACLE RMAN备份及还原(转)

    RMAN可以进行增量备份:数据库,表空间,数据文件 只有使用过的block可以被备份成backup set 表空间与数据文件对应关系:dba_data_files / v$datafile_heade ...

  8. rlwrap与历史命令

    # yum install rlwrap $ vi .bash_profile alias sqlplus='rlwrap sqlplus'alias rman='rlwrap rman' 或者 l ...

  9. Android GridView使用View.GONE只隐藏内容而不隐藏空间的解决方案

    最近在处理GridView的时候遇到这样一个问题:Android手机客户端接收服务端返回的一串数据(数据条数不固定),这串数据不一定都要显示到GridView上,也就是说有一部分内容需要隐藏掉,即有一 ...

  10. 大数据入门第二十四天——SparkStreaming(一)入门与示例

    一.概述 1.什么是spark streaming Spark Streaming is an extension of the core Spark API that enables scalabl ...