HDU 1238 Substrings

来源:互联网 发布:sql server恢复数据库 编辑:程序博客网 时间:2024/06/11 19:40

Substrings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8866    Accepted Submission(s): 4157

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 3ABCD BCDFFBRCD 2roseorchid
Sample Output
22

 

Author
Asia 2002, Tehran (Iran), Preliminary
题意是给定少于100个字符串,每个字符串至多100个字符,求问是否存在一个最长的子串,使得这个子串存在于所有的给定的串中或者是子串的反串在所有的给定的串中存在。
这个时间给的数据量比较小,可以直接暴力的枚举,然后利用string的find来进行匹配。
代码如下:
/*************************************************************************    > File Name: Substrings.cpp    > Author: Zhanghaoran    > Mail: chilumanxi@xiyoulinux.org    > Created Time: 2016年02月23日 星期二 12时39分07秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <string>using namespace std;int T;int n;string str[110];int main(void){    cin >> T;    while(T --){        cin >> n;        int len = 110;        int len_pos = 0;        for(int i = 0; i < n; i ++){            cin >> str[i];            if(len > str[i].size()){                len = str[i].size();                len_pos = i;            }        }        int ans = 0;        bool flag = false;        for(int i = len; i >= 1 && !flag; i --){            for(int j = 0; i + j <= len && !flag; j ++){                string testa = str[len_pos].substr(j, i);                string temp = testa;                string testb = temp.assign(temp.rbegin(), temp.rend());                for(int k = 0; k < n; k ++){                    if(k == len_pos)                        continue;                    if(str[k].find(testa) == -1 && str[k].find(testb) == -1){                        flag = true;                        break;                    }                }                if(!flag){                    flag = true;                    ans = i;                }                else                    flag = false;            }        }        cout << ans << endl;    }}

 

查看原文:http://chilumanxi.org/2016/02/23/hdu-1238-substrings/

0 0
原创粉丝点击