Substrings
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10262 | Accepted: 3525 |
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
题目大意:找出一组字符串中的最长公共子串或逆序子串的长度
#include <string> #include <stdio.h> #include <stdlib.h> #include<iostream> #include<cstdio> #include<cstring> using namespace std; char srcstr[110][110];//输入字符串 char str[110];//比较字符串 char strrev1[110];//翻转后的比较字符串 char strshort[110];//最短的字符串 int n; int ncase; void StrRev(char* pstr) { int nLen = strlen(pstr); for (int i = nLen - 1; i >= 0; i--) { strrev1[nLen - i - 1] = pstr[i]; } strrev1[nLen] = '\0'; } int Find() { int nLenshort = strlen(strshort); int nLen = nLenshort; while(nLen) { for (int j = 0; j + nLen <= nLenshort; j++) { strncpy(str, strshort + j, nLen); str[nLen] = '\0'; StrRev(str); for (int k = 0; k < n; k++) { if (!strstr(srcstr[k], str) && !strstr(srcstr[k], strrev1)) { break; } if (k == n - 1) { return nLen; } } } nLen--; } return nLen; } int main() { scanf("%d", &ncase); int MinLen = 1000; while(ncase--) { MinLen = 1000; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", srcstr[i]); if (MinLen > strlen(srcstr[i])) { MinLen = strlen(srcstr[i]); strcpy(strshort, srcstr[i]); } } printf("%d\n", Find()); } return 0; }