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. SVN 远程访问

    第一种方法 https://www.cnblogs.com/Leo_wl/p/3475167.html#_label0 默认协议为:https 端口号:443 服务器地址:https://主机名/sv ...

  2. PyQt5---firstwindow

    # -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...

  3. 4.Dubbo2.5.3集群容错和负载均衡

    转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.集群容错和负载均衡原理 各节点关系: 这里的Invoker是Provider的一个可调用Service的抽 ...

  4. Oracle_spatial的常见错误与注意事项

    常见的错误 1.ORA-13226:没有空间索引接口将不被支持 当使用一个空间操作符时,如果没有使用空间索引导致该操作符不能被完成将会返回该错误.这可能会发生在当你使用的列上没有空间索引.或者优化器没 ...

  5. canvas实例_时钟

    效果图:是一个会动的时钟 一.时钟的组成 1.表盘(蓝色)  2.刻度(黑色)  3.时针(黑色)  4.分针(黑色)  5.秒针(红色)需美化     二.主要应用的技术 Canvas画线 Canv ...

  6. WorldWind源码剖析系列:地形瓦片类TerrainTile和地形瓦片服务类TerrainTileService

    地形瓦片类TerrainTile 用来抽象封装用户漫游中所请求的地形瓦片数据类型. 地形瓦片服务类TerrainTileService提供了从BIL(Binary Interleaved by Lin ...

  7. 如何为Windows Forms应用程序添加启动参数(Start-Up Parameters)

    很多场合下,我们需要通过命令行或者快捷方式在Windows Forms程序启动时向其传递参数. 这些参数可能是用来加载某一个文档,或者是应用程序的初始化配置文件. 特别是对那些需要高度自定义配置的大程 ...

  8. Java使用线程并发库模拟弹夹装弹以及发射子弹的过程

    同样是从网上看到的一个需求,需求描述都在代码中. 不多说了,直接贴代码了.相信大家都能够看得懂的! package cn.yw.bore; import java.util.ArrayList; im ...

  9. eclipse 格式化快捷键(Ctrl+shift+f)不起作用的解决办法

    eclipse格式化快界面Ctrl+Shift+f不起作用一般是键位冲突所导致的,一般是搜狗输入法的“繁体与简体”中文切换快界面冲突. 把它禁用掉就可以了. 下面是禁用步骤: 点击sougou输入法右 ...

  10. 【H5】滚动事件(jq)

    $(function(){ console.log($('html,body').scrollTop());  //记录滚动高度(滚动前) }) $('html,body').scroll(funct ...