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
 
题意:在给出的字符串中, 找到在所有字符串中均出现的最长子串。
 
 
思路 :先找出最短的字符串,再用其子串搜索, 目的是优化运行;
         因为要 求出最长子串, 首先从len长度的子串开始;
         子串长度依次减小, 保证所求定为最长子串; 
         多函数的组合有利于将问题细化。
 
 
 
 
 
 
 
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#define maxn 105
char lina[maxn];
char a[maxn][maxn];
int x;
void linstr () //找出长度最小的字串
{
int i;
int len = 10000, lens;
lina[0]='\0';
scanf("%d", &x);
for (i = 0; i<x; i++)
{
scanf("%s", a[i]);
int lens = strlen(a[i]);
if (len>lens)
{
strcpy(lina, a[i]);
len = lens;
}
} }
int fin (char str[], char rts[]) //判断所提取的子串是否在所有字符串中出现
{
int i; for (i = 0; i<x; i++)
{
if (strstr(a[i], str)==0 && strstr(a[i], rts)==0)
return 0;
}
return 1;
} int fuck ()
{
int i, len, j;
len = strlen (lina);
for (i = len; i>0; i--)
{
for (j = 0; j+i<=len; j++)
{
char str[maxn]= {0}, rts[maxn];
strncpy(str, lina+j, i); //将lina中第j个开始的i个字符cpy到str中
strcpy(rts, str);
strrev(rts); //倒置函数 为了方便看题才用的 好多oj不支持倒置函数 需要自己再写一个函数完成倒置
if (fin(str, rts)==1) //判断str和他的倒置函数是否满足条件
return i;
}
}
return 0;
}
int main ()
{
int n, num; scanf("%d", &n);
while (n--)
{
linstr();
num = fuck();
printf("%d\n", num);
}
return 0;
}

  

 

Substrings 子字符串-----搜索的更多相关文章

  1. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  2. [LeetCode] Count Binary Substrings 统计二进制子字符串

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  3. [LeetCode] Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  4. [LeetCode] 647. Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  5. 使用后缀数组寻找最长公共子字符串JavaScript版

    后缀数组很久很久以前就出现了,具体的概念读者自行搜索,小菜仅略知一二,不便讨论. 本文通过寻找两个字符串的最长公共子字符串,演示了后缀数组的经典应用. 首先需要说明,小菜实现的这个后缀数组算法,并非标 ...

  6. Swift3.0语言教程查找字符集和子字符串

    Swift3.0语言教程查找字符集和子字符串 Swift3.0语言教程查找字符集和子字符串,在字符串中当字符内容很多时,我们就需要使用到查找字符集或者子字符串的方法.以下我们将讲解3种查找字符集和子字 ...

  7. Java中String类常用方法(字符串中的子字符串的个数)

    重点内容 4种方法: 1.int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startInde ...

  8. C#下利用正则表达式实现字符串搜索功能的方法(转)

    关键字:正则表达式.元字符.字符串.匹配: 1.正则表达式简介:正则表达式提供了功能强大.灵活而又高效的方法来处:.NET框架正则表达式并入了其他正则表达式实现的: 2.字符串搜索:正则表达式语言由两 ...

  9. Java实现 LeetCode 730 统计不同回文子字符串(动态规划)

    730. 统计不同回文子字符串 给定一个字符串 S,找出 S 中不同的非空回文子序列个数,并返回该数字与 10^9 + 7 的模. 通过从 S 中删除 0 个或多个字符来获得子字符序列. 如果一个字符 ...

随机推荐

  1. [iOS]集成环信SDK然后运行时候crash了-[NSBundle initWithURL:]: nil URL argument'

    Crash的reason是-[NSBundle initWithURL:]: nil URL argument' 1.首先我是用cocoapods导入的环信的SDK.然后怎么运行怎么crash. 2. ...

  2. idea自动生成serialVersionUID

    Setting->Plugins 找到一个叫  GenerateSerialVersionUID 的插件 下载安装好,alt+insert就可以看到 默认情况下Intellij IDEA是关闭了 ...

  3. Spring整合JUnit4测试

    @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring/ap ...

  4. Linux系统VIM编辑器

    vim,linux系统中一款超好用的文本编辑器,是vi的升级版. 三种操作模式 命令模式: 控制光标移动,可对文本进行删除.恢复.黏贴等工作 输入模式: 正常的文本录入 末行模式: 保存,退出与设置编 ...

  5. CleanAOP实战系列--WPF中MVVM自动更新

    CleanAOP实战系列--WPF中MVVM自动更新 作者: 立地 邮箱: jarvin_g@126.com QQ: 511363759 CleanAOP介绍:https://github.com/J ...

  6. oracle11g卸载出错 无法删除文件,文件正在使用中

    在卸载oracle11g时 停止服务后,运行C:\myoracle\think\product\11.2.0\dbhome_2\deinstall 中的 deinstall.bat 可以在cmd中直接 ...

  7. html 用图片代替重置按钮

    提交时,若把按钮设置成图片提交特别方便只要 <input type="image" alt="点此提交" src="images/button. ...

  8. weka数据挖掘拾遗(三)----再谈如果何生成arff

    前一阵子写过一个arff的随笔,但是写完后发现有些啰嗦.其实如果使用weka自带的api,生成arff文件将变成一件很简单的事儿. 首先,可以先把特征文件生成csv格式的.csv格式就是每列数据都用逗 ...

  9. post提交/文件上传服务器修改

    第一步:修改在php5下POST文件大小的限制   1.编修php.ini   找到:max_execution_time = 30 ,这个是每个脚本运行的最长时间,单位秒,修改为: max_exec ...

  10. 用VS2005开发WinCE程序调试图文教程

    一.WinCE 模拟器通过ActiveSync 6.1(即Windows Mobile设备中心)连接P 1.启动WinCE模拟器 命令行:  start .\DeviceEmulator.exe WI ...