LUCKY STRING

来源:互联网 发布:ubuntu启动引导器设置 编辑:程序博客网 时间:2024/06/11 14:36

题目描述

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
import java.util.HashSet;import java.util.Scanner;import java.util.Set;import java.util.TreeSet;public class Main {private TreeSet<String> result = new TreeSet<String>();private Set<Integer> fn = new HashSet<Integer>();public static void main(String[] args) {// TODO Auto-generated method stubScanner reader = new Scanner(System.in);String str = reader.next();if(str==null||"".equals(str)) return;Main ls = new Main();ls.luckNumber(str);}public void luckNumber(String str){ char[] tmp = str.toCharArray(); int n = tmp.length; calFn(n); for(int i=0; i<n; ++i){ for(int j=1; i+j<=n; ++j){ String t = new String(tmp, i, j); if(!result.contains(t)){ if(isLuckNumber(t)){ result.add(t); } } } }  for(String w : result){ System.out.println(w); }  }  private boolean isLuckNumber(String str){ Set<Character> set = new HashSet<Character>(); for(int i=0;i<str.length();++i){ set.add(str.charAt(i)); } int len = set.size(); if(fn.contains(len)){ return true; }else{ return false; } }  public void calFn(int n){ fn.add(1); int a = 1; int b = 1; int c = 0; for(int i=2;i<=n;++i){ c = a + b; fn.add(c); a = b; b = c; } }}


0 0
原创粉丝点击