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. zw版【转发·台湾nvp系列Delphi例程】HALCON OverpaintRegion2

    zw版[转发·台湾nvp系列Delphi例程]HALCON OverpaintRegion2 unit Unit1;interfaceuses Windows, Messages, SysUtils, ...

  2. jboss-as 目录结构(转)

    jboss-as 目录结构(Directory Structure) Directory Description bin Contains startup, shutdown and other sy ...

  3. Openstack的镜像上传原理

    openstack的horizon的上传镜像流程 通过html的form表单上传文件 先上传到horizon指定的临时目录,存储起来 通过glance-api请求接口 实际上glance-api也是提 ...

  4. 指针二次释放(_BLOCK_TYPE_IS_VALID)

    [1]_BLOCK_TYPE_IS_VALID是什么错误? (1)最简单的示例代码如下: void main() { ); delete pA; delete pA; } (2)运行后崩溃截图如下: ...

  5. iOS原生JSON解析.

    - (IBAction)accessInterfaceBtnPressed:(id)sender {        NSError *error;    NSString *URL=@"ht ...

  6. webpack笔记_(2)_Refusing to install webpack as a dependency of itself

    安装webpack时,出现以下问题: Refusing to install webpack as a dependency of itself npm ERR! Windows_NT npm ERR ...

  7. test if DEMO

    可参考:http://blog.chinaunix.net/uid-20671208-id-3643362.html 1.test 举例: test -d ~/auto && echo ...

  8. iOS直播的技术分析与实现

    HTTP Live Streaming直播(iOS直播)技术分析与实现 发布于:2014-05-28 13:30阅读数:12004 HTTP Live Streaming直播(iOS直播)技术分析与实 ...

  9. expect语法

    沙河西ftp上传,使用了expect语言的脚本. 我们经常会遇到一些需要与服务器程序打交道的场景,比如,从登陆某个服务器,然后进行某项工作.这很平常,但是如果把这个工作自动化进行,你就需要一个程序能自 ...

  10. 发送xml报文去第三方请求获取xml报文数据

    import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import j ...