题目链接:http://hihocoder.com/problemset/problem/1152
题意:给一个字符串,找所有的满足不同字符数为斐波那契数的子串。
暴力,枚举。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 111; 5 const int maxm = 66; 6 int n; 7 int fib[maxm]; 8 char s[maxn]; 9 set<string> ret; 10 set<int> vis; 11 12 bool ok(int l, int r) { 13 set<char> t; 14 for(int i = l; i <= r; i++) t.insert(s[i]); 15 if(vis.find(t.size()) != vis.end()) return 1; 16 return 0; 17 } 18 19 int main() { 20 // freopen("in", "r", stdin); 21 char buf[maxn]; 22 memset(fib, 0, sizeof(fib)); 23 vis.clear(); 24 fib[0] = fib[1] = fib[2] = 1; 25 fib[3] = 2; 26 vis.insert(1); 27 vis.insert(2); 28 for(int i = 4; i < maxm; i++) { 29 fib[i] = fib[i-1] + fib[i-2]; 30 vis.insert(fib[i]); 31 } 32 while(~scanf("%s", s)) { 33 n = strlen(s); 34 ret.clear(); 35 for(int l = 0; l < n; l++) { 36 for(int r = l; r < n; r++) { 37 if(ok(l, r)) { 38 memset(buf, 0, sizeof(buf)); 39 for(int i = l; i <= r; i++) buf[i-l] = s[i]; 40 ret.insert(buf); 41 } 42 } 43 } 44 for(set<string>::iterator it = ret.begin(); it != ret.end(); it++) { 45 cout << *it << endl; 46 } 47 } 48 return 0; 49 }