• 微软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);
        }
    }


  • 相关阅读:
    leetcode-832-Flipping an Image
    leetcode-830-Positions of Large Groups
    leetcode-824-Goat Latin(字符串的处理)
    leetcode-821-Shortest Distance to a Character
    leetcode-819-Most Common Word(词频统计)
    HDU 4729 An Easy Problem for Elfness(树链剖分边权+二分)
    python爬虫(1)——正则表达式
    利用MySQL之federated引擎实现DBLink功能
    HTTPS原理及流程
    NIO、多路复用的终极奥义
  • 原文地址:https://www.cnblogs.com/stonehat/p/4470452.html
Copyright © 2020-2023  润新知