Substrings--poj1226(字符串)
2024-09-07 10:52:02
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 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 题目大意:
给你n个字符串 要你找这n个字符串中最大的相同字串(字符串可以逆置) 分析:
先找到最小的母串 然后这个母串的所有字串从大到小 都和n个串循环一遍 只要找到符合的答案就跳出
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#define N 220
#define INF 0xffffffff
using namespace std; int main()
{
int T,n;
char str[N][N],s1[N],s2[N];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int Min=INF,b;
for(int i=; i<n; i++)
{
scanf("%s",str[i]);
if(strlen(str[i])<Min)
{
Min=strlen(str[i]);
b=i;
}
}
int len=Min,flag;
while(len)
{
for(int i=; i<=Min-len; i++)
{
flag=;
strncpy(s1,str[b]+i,len);
int k=;
for(int i=len-; i>=; i--)
{
s2[k++]=s1[i];
}
s1[len]=s2[len]='\0';
for(int i=; i<n; i++)
{
if(strstr(str[i],s1)==NULL && strstr(str[i],s2)==NULL)
{
flag=;
break;
}
}
if(flag==)
{
break;
}
}
if(flag==)
break;
len--;
}
printf("%d\n",len);
}
return ;
}
最新文章
- 适配器模式/adapter模式/结构型模式
- VS联调多个解决方案的项目
- [Chapter 3 Process]Practice 3.4 Describe what happens when a context switch occurs if the new context is already loaded into one of the register sets.
- Android 4.4 音量调节流程分析(二)
- 很棒的jQuery代码片段分享
- 【转】 android 4.4 Step Counter Sensor计步器的使用
- SQL子句执行顺序和Join的一点总结
- CentOS Apache服务器安装与配置
- 关于Android 7.0无法进行https抓包的问题
- security cookie 机制(2)--- 初始化___security_cookie
- js数组中两个有相同删除一个
- 设置服务器的MySQL允许远程访问/外网访问
- 5、Python-字典
- ubuntu上zip格式解压乱码解决
- Python基础爬虫
- excel中如何将时间戳转换为日期格式
- Cocoa Touch(二):数据存储CoreData, NSKeyArchiver, NSOutputStream, NSUserDefaults
- html 中的列表
- Nginx+keepalived 脚本安装主从双机热备自动切换解决方案
- [Z] 关于Python Tornado的一些资料