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. SDN 第二次上机作业

    SDN第二次上机作业 1.控制器floodlight所示可视化图形拓扑的截图,及主机拓扑连通性检测截图 拓扑 连通性 2.利用字符界面下发流表,使得'h1'和'h2' ping 不通 流表截图 连通性 ...

  2. jstl格式化日期

    <%@ page contentType="text/html" pageEncoding="GBK"%> <%@ page import=& ...

  3. 开发jQuery插件的基本步骤

    在进行开发jQuery插件前,首先要了解一些知识: 1.闭包 1.1.闭包的作用: · 避免全局依赖 · 避免第三方破坏 · 兼容jQuery操作符'$'和jQuery 1.2.闭包的形式 (func ...

  4. PHP+Hadoop+Hive+Thrift+Mysql实现数据统计分析

    原址:http://www.cnblogs.com/wicub/p/6094045.html   安装 Hadoop安装: http://www.powerxing.com/install-hadoo ...

  5. 矿难让显卡压了那么多货咋办?NV如是说

    在苏州 GTC 开幕的几天前,英伟达刚刚遭遇了一次股价的腰斩. 近来加密货币的热度渐低,受到挖矿热潮照顾许多的英伟达「矿机」销量受到打击,甚至出现了严重的库存危机,加上近来刚刚发的 RTX20 系列显 ...

  6. RC Calculation

    scenario 定义中包括 Mode.Corner.RC 其中 Corner (PVT)用于计算 cell delay 而 RC 用于计算 net delay 本文简要介绍如何使用 RC 参数来计算 ...

  7. Python2.7-shutil

    shutil 模块,高级文件操作模块,支持文件或文件夹的复制和删除 模块方法: shutil.copyfileobj(fsrc, fdst[, length]):将类文件对象(在python中打开的文 ...

  8. java操作数据库的事务支持

    一.需求背景: 我们生活经常遇到一个情况:在购买商品的时候,已经支付的了,那么商品应该处于已购买订单里.而不是付款之后,已购买商品没有. 还有转账的时候,转出方和转入方都需要扣减相应的金额,而不是一方 ...

  9. JavaScript 删除数组中的对象

    1.获得对象在数组中的下标 function (_arr,_obj) { var len = _arr.length; for(var i = 0; i < len; i++){ if(_arr ...

  10. OpenGL笔记(二) 渲染管线

    GLSL着色器代码分为两个部分,即Vertex Shader(顶点着色器)与Fragment Shader(片元着色器). 上下文环境搭建 OGL不负责窗口管理与上下文环境管理,该职责由平台完成.在A ...