DP use HashMap:

根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain,则更新

 package twoSigma;

 import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.lang.StringBuilder; public class LongestChain {
public int findLongestChain(String[] words) {
if (words==null || words.length==0) return 0;
int longestChain = 0;
Arrays.sort(words, new Comparator<String>() {
public int compare(String str1, String str2) {
return str1.length()-str2.length();
}
});
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String word : words) {
if (map.containsKey(word)) continue;
map.put(word, 1);
for (int i=0; i<word.length(); i++) {
StringBuilder sb = new StringBuilder(word);
sb.deleteCharAt(i);
String after = sb.toString();
if (map.containsKey(after) && map.get(after)+1 > map.get(word)) {
map.put(word, map.get(after)+1);
}
}
if (map.get(word) > longestChain) longestChain = map.get(word);
}
return longestChain;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LongestChain sol = new LongestChain();
//String[] words = new String[]{"6", "a", "b", "ba", "bca", "bda", "bdca"};
//String[] words = new String[]{"a", "a", "bc", "exf", "abc"};
String[] words = new String[]{"bc", "abc"};
int longestChain = sol.findLongestChain(words);
System.out.println(longestChain);
} }

2Sigma OA prepare: Longest Chain的更多相关文章

  1. 2Sigma OA prepare: Friends Circle

    DFS & BFS: 关键在于构造graph package twoSigma; import java.util.ArrayList; import java.util.HashSet; i ...

  2. G面经Prepare: Longest All One Substring

    give a string, all 1 or 0, we can flip a 0 to 1, find the longest 1 substring after the flipping 这是一 ...

  3. Twitter OA prepare: Two Operations

    准备T家OA,网上看的面经 最直接的方法,从target降到1,如果是奇数就减一,偶数就除2 public static void main(String[] args) { int a = shor ...

  4. UvaLive 6667 Longest Chain (分治求三元组LIS&amp;树状数组)

    题目链接: here 题意: 和hdu4742类似.差别就是一部分三元组是直接给出的.另一部分是用他给的那个函数生成的.还有就是这里的大于是严格的大于a>b必须ax>bx,ay>by ...

  5. Twitter OA prepare: even sum pairs

    思路:无非就是扫描一遍记录奇数和偶数各自的个数,比如为M和N,然后就是奇数里面选两个.偶数里面选两个,答案就是M(M-1)/2 + N(N-1)/2

  6. Twitter OA prepare: K-complementary pair

    2sum的夹逼算法,需要sort一下.本身不难,但是tricky的地方在于允许同一个数组元素自己跟自己组成一个pair,比如上例中的[5, 5].而且数组本身就允许值相等的元素存在,在计算pair时, ...

  7. Twitter OA prepare: Anagram is A Palindrome

    Algorithm: Count the number of occurrence of each character. Only one character with odd occurrence ...

  8. Twitter OA prepare: Visit element of the array

    分析:就是建立一个boolean array来记录array里面每个元素的访问情况,遇到访问过的元素就停止visiting,返回未访问的结点个数 public int visiting(int[] A ...

  9. Twitter OA prepare: Rational Sum

    In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q ...

随机推荐

  1. 【ZOJ】3329 One Person Game

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题意:有三个色子,分别有k1.k2.k3个面,权值分别是1-k1, 1~ ...

  2. 【bzoj2179】FFT快速傅立叶 FFT模板

    2016-06-01  09:34:54 很久很久很久以前写的了... 今天又比较了一下效率,貌似手写复数要快很多. 贴一下模板: #include<iostream> #include& ...

  3. tableFooterView中的按钮点击没反应

    一,经历 1.查了按钮没有响应的几个方法,排除了是用户交互设置为 NO 的情况. 2.然后打印了一下tableFooterView,尽然发现其高度为0,而且我也没有设置 frame, 却可以显示按钮, ...

  4. C#中Thread.sleep() 【转载】

    我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题: 1.假设现在是 2008-4-7 12:00:00.000,如果我 ...

  5. JAVA复制网络图片到本地

    import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.Out ...

  6. Fling——K

    K. Fling Fling is a kind of puzzle games available on phone.This game is played on a board with 7 ro ...

  7. Eclipse for Mac 常用快捷键

    为了提高开发效率,Eclipse 为我们提供了许多快捷键,它们能够帮助我们快速和方便的完成一些繁琐的操作.在这里只提供 Eclipse for Mac 的常用快捷键. Command + O:显示大纲 ...

  8. 第1章 Bootstrap介绍

    学习网址:http://www.bootcss.com/ 下载网址:http://v3.bootcss.com/ 下载后一共三个文件夹 css js fonts 引入的文件 <link rel= ...

  9. HTML--JS练习小游戏(别踩白块儿)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Mysql的一些常用命令

    Mysql基本操作 创建表: create table test01_02(id varchar(50) not null auto_increment primary key, name nvarc ...