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. WINDOWS SOCKET编程中accept出来的新连接是阻塞还是非阻塞

    实践证明 SOCKET hNewSock=accept(hListenSock) 当hListenSock为阻塞模型时,hNewSock则为阻塞模型 否则 当hListenSock为非阻塞模型时,hN ...

  2. SDN 第四次作业

    阅读 了解SDN控制器的发展 http://www.sdnlab.com/13306.html http://www.docin.com/p-1536626509.html 了解ryu控制器 http ...

  3. Phoenix安装配置

    下载Phoenix     在网站http://phoenix.apache.org/download.html找到对应HBase版本的安装程序,并下载安装包,解压安装程序到指定目录 [root@ha ...

  4. win10管理员已阻止你运行此应用”解决方法

    方法/步骤 1 按WIN+R键,打开“运行”,然后输入“gpedit.msc",就是打开组策略,这个在控制面板中也可以打开. 2 在组策略里找到“计算机配置”-“Windows设置”-“安全 ...

  5. Eclipse Mars 2安装Drools6.4插件(Drools and jBPM tools)时无法安装JBoss Runtime Drools Detector

    在eclipse上本地安装Drools6.4Final的时候出现两个组件无法正常安装的情况,具体组件如下: 具体的提示信息为: Cannot complete the install because ...

  6. Node.js实战(十一)之Buffer

    JavaScript 语言自身只有字符串数据类型,没有二进制数据类型. 但在处理像TCP流或文件流时,必须使用到二进制数据.因此在 Node.js中,定义了一个 Buffer 类,该类用来创建一个专门 ...

  7. Nginx端口占用问题

    错误信息:nginx: [emerg] listen() to 0.0.0.0:80, backlog 511 failed (98: Address already in use) 主要是端口被占用 ...

  8. Python2.7-decimal

    decimal 模块,提供了对小数精确的计算,内置的 float 类型是以二进制的浮点数保存的,是不准确的,小数点后会有很多奇怪的数字,虽然在一般情况下计算是没问题的,因为近似相等,小数点后十几位才会 ...

  9. keepalived 做全端口映射

    global_defs {    lvs_id BACKUP }   vrrp_sync_group VGM {     group {        VI_1     } }   vrrp_inst ...

  10. MVC的BundleConfig应用

    1.MVC可以通过BundleConfig类来配置css和js的统一引用,分别通过StyleBundle和ScriptBundle来创建. 2.可以在母版页中统一加载设置在BundleConfig.c ...