首页 > 试题广场 >

LUCKY STRING

[编程题]LUCKY STRING
  • 热度指数:12047 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.
示例1

输入

aabcd

输出

a 
aa 
aab 
aabc 
ab 
abc 
b 
bc 
bcd 
c 
cd 
d
import java.util.*;
public class Main
    {
    public static void main(String[] args)
     {
     Scanner sc = new Scanner(System.in);
     String str = sc.next();
     sc.close();  //
     int  [] fib={1,2,3,5,8,13,21,34,55,89};
        //找子串
     Set <String> ss=new HashSet<String>();
     for(int j=0;j<str.length();j++)
         {
          for(int k=j;k<str.length();k++)
              {
              ss.add(str.substring(j,k+1));
          }
     }
        //串排序
       List<String > list= new ArrayList<String>(); 
       list.addAll(ss);
       Collections.sort(list);
      //寻找不同的字符串个数
          Set<Character>  chs =new HashSet<Character>();
        for(String s : list)
            {
             char[] ch=s.toCharArray();
            for( char c: ch)
                chs.add(c);
            
            for(int i=0;i<fib.length;i++)
                {
                if(chs.size()==fib[i])
                     System.out.println(s);//330,17640k
            }
          chs.clear();
        }
 }
 }


发表于 2017-07-08 11:41:24 回复(0)

LUCKY STRING

思路

  1. 构造100以内的fibonacci数。
  2. 取substring,判断是否是fibonacci数,可以用set保存substring的不同字符。

代码

public static void main(String[] args) {
        // 输入
        Scanner in = new Scanner(System.in);
        String s = in.next();

        // 构造fibonacci数set
        // 输入字符串不多于100,所以只需要构造100以内
        Set<Integer> fib = new HashSet<>();
        int a = 1;
        int b = 1;
        for (int i = 1; i <= 20; i++) {
            if (b > 100) break;
            fib.add(b);
            int sum = a + b;
            a = b;
            b = sum;
        }

        // 用来保存子串中的字符,set的size代表不同字符的个数
        Set<Character> temp = new HashSet<>();
        // treeSet用来排序结果
        Set<String> res = new TreeSet<>();
        for (int i = 0; i < s.length(); i++) {
            temp.add(s.charAt(i));
            for (int j = i; j < s.length(); j++) {
                if (j > i) temp.add(s.charAt(j));
                // 检查该数子是否是fibonacci数
                if (fib.contains(temp.size())) res.add(s.substring(i, j + 1));
            }
            temp.clear();
        }

        // 输出
        for (String s1 : res) {
            System.out.println(s1);
        }
    }
发表于 2017-05-20 22:33:28 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String str = s.nextLine();
        int l = str.length();
        if(l<=1) {
            System.out.print(str);
        }
        Set<String> splice_str = new HashSet<>();
        for(int i=0; i<l; i++) {
            for(int j=i; j<l; j++) {
                splice_str.add(str.substring(i, j+1));
            }
        }
        ArrayList<String> res = new ArrayList<>();
        res.addAll(splice_str);
        Collections.sort(res);
        for(int i=0; i<res.size(); i++) {
            String t = res.get(i);
//转换排序
            char[] ch = t.toCharArray();
            Arrays.sort(ch);
            int count=1;
            char c = ch[0];
//去重
            for(int j=1; j<ch.length; j++) {
                if(c!=ch[j])
                    count++;
                c = ch[j];
            }
            if(isFibonacci(count))
                System.out.println(t);
        }
    }
//判断是否为斐波拉契数
    public static boolean isFibonacci(int n) {
        if(n==1)
            return true;
        int[] a = new int[100];
        a[0] = 1;
        a[1] = 1;
        while(true) {
            if(n==a[1]) {
                return true;
            }
            else if(n>a[0] && n<a[1])
                return false;
            int t = a[1];
            a[1] += a[0];
            a[0] = t;
        }
    }
}

发表于 2017-04-05 09:16:38 回复(0)
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String s = sc.next();
			Set<String> set = new TreeSet<String>();
			for (int i = 0; i < s.length(); i ++ ) {
				for (int j = i; j < s.length(); j ++ ) {
					String temp = s.substring(i, j + 1);
					if(isFibonacci(temp)) set.add(temp);
				}
			}
			for (String result:set)
				System.out.println(result);
		}
	}

	private static boolean isFibonacci(String temp) {
		int[] fibonacci = {1, 2, 3, 5, 8, 13, 21};
		boolean flag = false;
		Set<Character> set = new HashSet<Character>();
		for (int i = 0; i < temp.length(); i ++ )
			set.add(temp.charAt(i));
		for (int i:fibonacci) {
			if(set.size() == i) {
				flag = true;
				break;
			} else
				continue;
		}
		return flag;
	}
}

编辑于 2016-08-29 21:35:57 回复(0)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in =new Scanner(System.in);
		String s=in.nextLine();
		char[] chars=s.toCharArray();
		ArrayList<String> a=new ArrayList();
		for(int i=0;i<chars.length;i++){
			int pre=1;
			int current=1;
			//System.out.println(chars[i]);
			while(current<=chars.length-i){
				for(int j=i+current-1;j<chars.length;j++){
					if(disnumber(chars,i,j)==current){
						if(!a.contains(sprint(chars,i,j))){
							a.add(sprint(chars,i,j));
						}
					}
				}
				int temp=current;
				current=current+pre;
				pre=temp;
				//System.out.println(current+"current");
			}
			
		}
		Collections.sort(a);
		for(int i=0;i<a.size();i++){
			System.out.println(a.get(i));
		}
			
		
	}
	public static int disnumber(char[] s,int start,int end){
		int count=0;
		for(int i=start;i<=end;i++){
			int isdifferent=0;
			for(int j=i+1;j<=end;j++){
				if(s[i]==s[j]){
					isdifferent=1;
					break;
					}
			}
			if(isdifferent==0)
				count++;
		}
		return count++;
	}
	public static String sprint(char[] s,int start,int end){
		char[] a=new char[end-start+1];
		 for(int i=start;i<=end;i++){
			a[i-start]=s[i];
		 }
		 String b=new String(a);
		 return b;
	}
}
思路:感觉最重要的还是要理解题目的意思,首先是子串,子串的含义就是字符串相对于原串的连续性,还有一个就是去重和字典排序。
用了多层循坏,从第一个字符往后找,含有不同的斐波拉契数的子串,依次找下去,再通过动态数组判断是否存在相同的字符串如果没有就添加。
循环结束之后对数组进行字典排序。

发表于 2016-08-26 11:07:03 回复(0)
//step 1: get candidate substring
//step 2: judge the substring  using hahSet //step 3: sorted by dict using java.util.Collections.sort(List)
package com.honghu.leetCode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.ArrayList;
import java.util.Collections;
public class LuckString {
	public static boolean IsLuckStr(String str)
	{
		Map<Character,Integer> map = new HashMap<>();
		for(int i = 0;i < str.length();i++)
		{
				map.put(str.charAt(i),1);
		}
		if(isFib(map.size())) // is fib
		{
			return true;
		}
		return false;
	}
	//fib数列
	public static int getFib(int fib)
	{
		if(fib == 2 || fib ==1)
		{
			return 1;
		}
		return getFib(fib-1)+getFib(fib-2);
	}
	//判断num是否是fib数列
	public static boolean isFib(int num)
	{
		int n = num;
		if(num < 5) //the feature of fibonacci number ,when num < 5,fib(num)<=num
		{
			n = 5;
		}
		boolean f = false;
		for(int i = 1;i<=num;i++)
		{
			int fib = getFib(i);
			if(fib == num)
			{
				f = true;
				break;
			}
		}
		return f;
	}
	public static void main(String[] args) {
		//String str = "nwlrbbmqbhcdarzowkkyhiddqscdxrjmowfrxsjybldbefsarcbynecdyggxxpklorellnmpapqfwkhopkmcoqhnwnkuewhsqmgb";
		Scanner in = new Scanner(System.in);
		String str = in.nextLine();
		Set<String>  arrayList = new HashSet();
		for(int i = 0;i<str.length();i++)
		{
			for(int j = 0;j < str.length() && ( str.length()-j> i);j++)
			{
				String tmp = str.substring(i, str.length()-j); //get candidate substring
				if(IsLuckStr(tmp))
				{
					arrayList.add(tmp);
				}
			}
		}
		List list = new ArrayList();
		for(String s:arrayList)
		{
			list.add(s);
		}
		Collections.sort(list);//dict based sort 
		for(Object str2:list)
		{
			System.out.println(str2); 
		}
}
}

编辑于 2016-07-17 17:51:25 回复(0)