微软2016校园招聘在线笔试第二场 A Lucky Substrings
描述
A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Given a string consisting of only lower case letters, output all its lucky non-empty substrings in lexicographical order. Same substrings should be printed once.
输入
A string consisting no more than 100 lower case letters.
输出
Output the lucky substrings in lexicographical order, one per line. Same substrings should be printed once.
- 样例输入
-
aabcd
- 样例输出
-
a aa aab aabc ab abc b bc bcd c cd d
注意:aabcd是一个特殊的事例:#include <iostream>
#include<cstring>
#include <algorithm>
using namespace std;
bool f(string s1,string s2)
{
if(s1<s2)return true;
else return false;
}
int m=0;
string ss[100000];
int judge[11]={0,1,2,3,5,8,13,21};bool p1(string ss1){
for(int i1=0;i1<m;i1++)
if(ss1==ss[i1]){return 0;}
return 1;}
bool p(string s2){
if(s2.length()==1)return 1;
int sum=0;
int n1=s2.length();
int a[26];
memset(a,0,sizeof(a));
for(int i=0;i<n1;i++){
if(a[s2[i]-'a']==0){a[s2[i]-'a']=1;sum++;}
}
for(int i=0;i<8;i++)
if(sum==judge[i])return 1;return 0;
}
int main(){
string s;
cin>>s;
int n=s.length();
for(int i=0;i<n;i++){
int j=1;
while(s[i+j-1]==s[i+j-2]){
j++;
}
for(;j<n-i+1;j++){string s1=s.substr(i,j);
if (p(s1)&&p1(s1))
ss[m++]=s1;}
}
sort(ss,ss+m,f);
for(int i=0;i<m;i++)
cout<<ss[i]<<endl;
return 0;}