首页 > 代码库 > HDU 1238 Substrings (水)
HDU 1238 Substrings (水)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1238
枚举最短字符串的的每个子串然后暴力。。。。我能怎么办,我也很无奈啊
代码:
1 #define _CRT_SECURE_NO_WARNINGS
2 #include <functional>
3 #include <algorithm>
4 #include <iostream>
5 #include <cstring>
6 #include <cassert>
7 #include <cstdio>
8 #include <cctype>
9 #include <vector>
10 #include <string>
11 #include <queue>
12 #include <stack>
13 #include <cmath>
14 #include <map>
15 #include <set>
16 using namespace std;
17 #define rep(i,a,n) for (int i=a;i<n;i++)
18 #define per(i,a,n) for (int i=n-1;i>=a;i--)
19 #define pb push_back
20 #define mp make_pair
21 #define all(x) (x).begin(),(x).end()
22 #define fi first
23 #define se second
24 #define SZ(x) ((int)(x).size())
25 typedef vector<int> VI;
26 typedef long long ll;
27 typedef pair<int, int> PII;
28 const ll mod = 1000000007;
29 ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res*a%mod; a = a*a%mod; }return res; }
30 // head
31 const int inf = 0x3f3f3f3f;
32 #define maxn 105
33 char s[maxn][maxn];
34 int n;
35
36 bool sstr(char tms[maxn], int tmpos){
37 bool flag = false;
38
39 for(int k = 0; k < n; k++){
40 if(k == tmpos) continue;
41 int tmslen = strlen(tms), i = 0, j = 0;
42 int tmbslen = strlen(s[k]);
43 bool tmflag = false;
44 for(int i = 0; i < tmbslen; i++){
45 int tmi = i;
46 while(s[k][i] == tms[j] && j < tmslen){
47 i++, j++;
48 }
49 if(j != tmslen){
50 j = 0;
51 i = tmi;
52 }
53 else{
54 tmflag = true;
55 break;
56 }
57 }
58 if(!tmflag)
59 return false;
60 }
61 return true;
62 }
63
64 int main(){
65 int t;
66 scanf("%d", &t);
67 while(t--){
68 memset(s, 0, sizeof(s));
69 scanf("%d", &n);
70 int minlen = inf, mini;
71 for(int i = 0; i < n; i++){
72 scanf("%s", s[i]);
73 int tmlen = strlen(s[i]);
74 if(tmlen < minlen){
75 minlen = tmlen;
76 mini = i;
77 }
78 }
79 int ans = 0, len = minlen;
80 char ss[maxn]; memcpy(ss, s[mini], sizeof(s[mini]));
81
82 for(int i = 0; i < len; i++){
83 for(int j = i; j < len; j++){
84 //temporary string
85 int tmlen = j - i + 1;
86 char tms[maxn];
87 strncpy(tms, ss + i, tmlen);
88 tms[tmlen] = ‘\0‘;
89 if(sstr(tms, mini)){
90 ans = max(ans, tmlen);
91 }
92 //swap the string
93 for(int k = 0; k < (tmlen) / 2; k++){
94 swap(tms[k], tms[tmlen - k - 1]);
95 }
96 if(sstr(tms, mini)){
97 ans = max(ans, tmlen);
98 }
99 }
100 }
101 printf("%d\n", ans);
102 }
103
104 }
题目:
Substrings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10400 Accepted Submission(s): 4959
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
Author
Asia 2002, Tehran (Iran), Preliminary
HDU 1238 Substrings (水)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。