Lucky String -- 微软笔试

标签(空格分隔): 算法


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

描述:

一个字符串是Lucky的当且仅当它里面字符的数目为Fibonacci数列中的一个数。如果给出一个字符串,它里面只包含小写字母,输出它所有的除空字符串外的Lucky SubString,每个子串只能被输出一次。并且输出的子串按照字典序列排序。

思路:

对于整个字符:

  1. 依次遍历字符串,截取子串
  2. 统计子串中的字符种类数,判断是否已经在结果集中出现或者是否符合Fibonacci数列
  3. 调用集合类的字符串排序算法,按照字典序列将结果集输出。

代码如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner; public class LuckyString { /**
* 给定一个字符串s,统计它里面一共出现了多少种字符
* @param s
* @return
*/
public static int count(String s) {
char[] ch = s.toCharArray();
boolean[] letter = new boolean[26];
for(int i=0; i<26; i++)
letter[i] = false;
int res = 0;
for(int i=0; i<ch.length; i++) {
if(letter[ch[i]-'a'] == false) {
letter[ch[i] -'a'] = true;
res++;
}
}
return res;
} /**
* 二分查找一个数是否在给定数组中出现
* @param nums
* @param n
* @return
*/
public static int binearySearch(int[] nums, int n) {
int left = 0, right = nums.length-1, mid = (left+right)/2;
while(left <= right) {
if(nums[mid] < n) {
left = mid + 1;
}
else if(nums[mid] > n)
right = mid - 1;
else return mid;
mid = (left + right) / 2;
}
return -1;
} /**
* 解决问题的主要函数。
* 1. 依次遍历字符串,截取子串
* 2. 统计子串中的字符种类数,判断是否已经在结果集中出现或者是否符合Fibonacci数列
* 3. 调用集合类的字符串排序算法,按照字典序列将结果集输出。
* @param s
*/
public static void solve(String s) {
int[] fibo = {1,2,3,5,8,13,21,34,55,89};
List<String> res = new ArrayList<String>();
for(int i=0; i<s.length(); i++) {
for(int j=i+1; j<s.length()+1; j++) {
String sub = s.substring(i, j);
int num = count(sub);
if(binearySearch(fibo, num) >= 0 && !res.contains(sub)) //包含该数
res.add(sub);
}
}
Collections.sort(res);
for(int i=0; i<res.size(); i++)
System.out.println(res.get(i));
} public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String s = sc.nextLine();
solve(s);
} }
}

Lucky String的更多相关文章

  1. 【每天一道算法题】Lucky String

    A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Gi ...

  2. 微软在线测试之lucky string,有关斐波那契的题目都在此了

    解决方案: int _tmain(int argc,_TCHAR* argv[]) { size_t fib[] = {1,2,3,5,8,13,21,34}; string str,tempstr; ...

  3. Codeforces 110B-Lucky String(技能)

    B. Lucky String time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力

    D. DZY Loves Strings 题目连接: http://codeforces.com/contest/444/problem/D Description DZY loves strings ...

  5. Codeforces Beta Round 84 (Div. 2 Only)

    layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: ...

  6. 枚举 + 进制转换 --- hdu 4937 Lucky Number

    Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  7. CodeForces 146A Lucky Ticket

    Lucky Ticket Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  8. hihocoder 1152 Lucky Substrings

    #1152 : Lucky Substrings 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if and only if t ...

  9. codeforces 630C Lucky Numbers

    C. Lucky Numbers time limit per test 0.5 seconds memory limit per test 64 megabytes input standard i ...

随机推荐

  1. OpenGL在什么样的领域才是主角?

          从OpenGL入门到现在掌握OpenGL开发(仅仅是掌握而已).随着对OpenGL理解的加深,也一点点的了解OpenGL所涉及的行业,有些行业OpenGL是主角,有些行业OpenGL是配角 ...

  2. iBatis面试题

    1) Ibatis中使用like ‘%#filedName#%’ 时,有什么问题? 在xml映射文件中,如果直接按如上写法,会报异常:java.sql.SQLException: Invalid ar ...

  3. 专为物联网开发的开源操作系统Contiki(转)

    专为物联网开发的开源操作系统Contiki(转)  (2012-04-19 15:31:09) 原文网址:http://blog.sina.com.cn/s/blog_6de000c201010z7n ...

  4. jQuery.isNumeric() 和 js isNaN()

    jQuery.isNumeric( value ) Description: 判断指定参数是否是一个数字值(字符串形式的数字也符合条件),返回 true 或者 false. Example: $.is ...

  5. 坑爹的SQL ISNUMERIC

    select ISNUMERIC('01,02') ISNUMERIC返回 1,后面用patindex代替了...折腾/ select patindex('%[^0-9]%','01,02')

  6. HDU 2222:Keywords Search(AC自动机模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意 ...

  7. php socket通信(tcp/udp)

    注意 1.在socket_bind的时候ip地址不能真回环地址如127.0.0.1 2.server.php后台跑起来的时候 nohup php server.php > /var/tmp/a. ...

  8. Android实现推送方式解决方案(转)

    本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...

  9. wamp集成环境 开启rewrite伪静态支持

    什么是伪静态 伪静态就是:动态网页通过重写URL的方法实现去掉动态网页的参数,但在实际的网页目录中并没有必要实现存在重写的页面. 伪静态的目的 最主要的就是迎合搜索引擎方便搜索引擎蜘蛛(Spider) ...

  10. ACM题目————星际之门(一)

    描述 公元3000年,子虚帝国统领着N个星系,原先它们是靠近光束飞船来进行旅行的,近来,X博士发明了星际之门,它利用虫洞技术,一条虫洞可以连通任意的两个星系,使人们不必再待待便可立刻到达目的地. 帝国 ...