博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1238 Substrings
阅读量:6245 次
发布时间:2019-06-22

本文共 2306 字,大约阅读时间需要 7 分钟。

Problem 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

题意:n个字符串,找出这n个字符串中连续相同的最大子序列,子串的逆序也可以。

#include
#include
const int N=110;char str[N][N], resouce[N];int n;void Init() ///在输入数据时找到最短的那条字符串,只有这样才能保证找到的子串有可能是公共子串{ int i; scanf("%d", &n); scanf("%s", str[0]); strcpy(resouce, str[0]); for (i = 1; i < n; i++) { scanf("%s", str[i]); if (strlen(str[i]) < strlen(resouce)) strcpy(resouce, str[i]); }}int Judge(char sub[], char resub[]) ///判断找到的子串和其逆序是否是其它字符串的子串{ int i; for (i = 0; i < n; i++) { if (strstr(str[i], sub) == NULL && strstr(str[i], resub) == NULL) return 0; ///只要在一个字符串中没有找到该子串和其逆序,则该子串不是公共子序列 } return 1;}int Sub(){ int i, j, len1 = strlen(resouce), k, t; char sub[N], resub[N]; for (i = len1; i >= 1; i--) { for (j = 0; i+j <= len1; j++) ///从最长的子串查找,只要找到了属于所有字符串的子串,最长公共子序列就是该子串,便可停止查找 { memset(sub, 0, sizeof(sub)); ///最短字符串的子串 memset(resub, 0, sizeof(resub)); ///子串的逆序 k = 0; strncpy(sub, resouce+j, i); for (t = strlen(sub)-1; t >= 0; t--) resub[k++] = sub[t]; resub[k] = '\0'; if (Judge(sub, resub)) return strlen(sub); } } return 0;}int main (){ int T, len; scanf("%d", &T); while(T--) { memset(resouce, 0, sizeof(resouce)); Init(); len = Sub(); printf("%d\n", len); } return 0;}

 

转载于:https://www.cnblogs.com/syhandll/p/4738301.html

你可能感兴趣的文章
token 验证的逻辑
查看>>
机器学习算法之概率分类法
查看>>
phone8 in-app purchasing
查看>>
Git 常用命令
查看>>
基于CentOS 5.4搭建nginx+php+spawn-fcgi+mysql高性能php平台
查看>>
Java学习图
查看>>
【C++进阶:STL常见性质3】
查看>>
HDU 1507 Uncle Tom's Inherited Land*
查看>>
\u Unicode和汉字转化
查看>>
javascript易混淆的split()、splice()、slice()方法详解
查看>>
shared_ptr 知识汇总
查看>>
快速排序
查看>>
排版与缩写
查看>>
C#使用xpath查找xml节点信息
查看>>
简单的语句统计所有用户表尺寸大小
查看>>
作业四:个人项目---小学四则运算
查看>>
漂亮的按钮样式-button
查看>>
post请求方式的翻页爬取内容及思考
查看>>
VC++ MFC如何生成一个可串行化的类
查看>>
php 变量引用,函数引用
查看>>