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;
}

  

 

最新文章

  1. New Training Table
  2. C++笔试题(转)
  3. OUYA游戏开发核心技术剖析OUYA游戏入门示例——StarterKit
  4. Linux多线程之同步2 &mdash;&mdash; 生产者消费者模型
  5. Server Tomcat v7.0 Server at localhost failed to start.错误可能原因之一
  6. python通过webservice连接cmdbuild
  7. bzoj 1026 [SCOI2009]windy数(数位DP)
  8. HTML&amp;CSS基础学习笔记1.26-input重置表单
  9. Android appcompat备案
  10. RMAN备份之非归档模式下的备份
  11. java继承中的初始化顺序
  12. 【SqlServer系列】AS的用法
  13. linux mysql 修改 UTF-8编码
  14. Django学习笔记(1)--第一个项目
  15. [Swift]LeetCode349. 两个数组的交集 | Intersection of Two Arrays
  16. vue全家桶项目搭建(vue-cli 2.9.6+vue-router+vuex+axios)
  17. C#版 - Leetcode 13. 罗马数字转整数 - 题解
  18. jQuery登录界面的文本框焦点代码
  19. 【算法】LeetCode算法题-Remove Element
  20. echart自定义浮窗 增加点击事件

热门文章

  1. Python实现ssh批量登录并执行命令
  2. JVM垃圾回收机制总结(4) :新一代的垃圾回收算法
  3. Linux命令-cp
  4. git的使用--不错的博客【转】
  5. Flex通过Blazeds利用Remoteservice与后台java消息推送
  6. pycharm最新注册方法 pycharm最新激活方法 2016pycharm最新注册方法
  7. ios用户控件
  8. Android HTTPS(1)概念和简单示例
  9. cygwin的rebaseall失败
  10. [Golang]使用自建代理访问指定网站