zoukankan      html  css  js  c++  java
  • 微软2016校园招聘在线笔试第二场 题目1 : Lucky Substrings

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    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.TreeSet;
    
    
    
    public class Main {
        public static HashSet<Integer> fibsetHashSet;
        static{
            fibsetHashSet= new HashSet<Integer>();
            fibsetHashSet.add(0);
            fibsetHashSet.add(1);
            fibsetHashSet.add(2);
            fibsetHashSet.add(3);
            fibsetHashSet.add(5);
            fibsetHashSet.add(8);
            fibsetHashSet.add(13);
            fibsetHashSet.add(21);
            fibsetHashSet.add(34);
            fibsetHashSet.add(55);
            fibsetHashSet.add(89);
        }
        public static boolean isFib(String sub){
            
            
            int count[]=new int[26]; //全是零
            
            if(sub.length()==0)return false;
            
            for(int i=0; i<sub.length(); i++){
                char a=sub.charAt(i);
                count[a-'a']+=1;
            }
            int diff=0;
            for( int i=0; i<26; ++i){
                if(count[i]!=0)++diff;
            }
            
            for( int i=0; i<26; ++i){
                if(!fibsetHashSet.contains(diff))return false; //如果不符合
            }
            return true;
        }
        public static void main(String args[]){
            Scanner scanner=new Scanner(System.in);
            String string=scanner.next();
            //String string="aabcd";
            TreeSet<String> set = new TreeSet<String>();
    
            
            int len=string.length();
            for(int i=0; i<len; i++){
                for(int j=i+1; j<=len; j++){
                    String subString=string.substring(i, j);
                    if(isFib(subString)){
                        set.add(subString);
                    }
                }
            }
            for(String key: set){
                System.out.println(key);
            }
            //System.out.println(set);
        }
    }


  • 相关阅读:
    Codeforces Round #632 (Div. 2)
    Codeforces Round #630 (Div. 2)
    多项式全家桶
    Educational Codeforces Round 84 (Rated for Div. 2)
    【cf1186E】E. Vus the Cossack and a Field(找规律+递归)
    [CF847B] Preparing for Merge Sort
    [CF858D] Polycarp's phone book
    [CF911D] Inversion Counting
    [CF938C] Constructing Tests
    [CF960C] Subsequence Counting
  • 原文地址:https://www.cnblogs.com/stonehat/p/4470452.html
Copyright © 2011-2022 走看看