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. 【BZOJ】3239: Discrete Logging

    http://www.lydsy.com/JudgeOnline/problem.php?id=3239 题意:原题很清楚了= = #include <bits/stdc++.h> usi ...

  2. Codeforces Beta Round #5

    A题,无聊的题目. #include <cstdio> #include <string> #include <cstring> #include <cmat ...

  3. BZOJ3636: 教义问答手册

    Description “汉中沃野如关中,四五百里烟蒙蒙.黄云连天夏麦熟,水稻漠漠吹秋风.”——摘自 黄裳<汉中行>“泉岭精神不朽,汉中诸球永生.”——摘自<泉岭精神创立者语录> ...

  4. Bootstrap_让Bootstrap轮播插件carousel支持左右滑动手势的三种方法

    Bootstrap 的 carousel.js 插件并没有支持手势. 3种解决方案 : jQuery Mobile (http://jquerymobile.com/download/) $(&quo ...

  5. List<string>中的泛型委托

    我们先看List<T>.Sort().其定义是:public void Sort( Comparison<T> comparison ) 其要求传入的参数是Comparison ...

  6. OSG 自定义数据类型 关键帧动画

    OSG 自定义数据类型 关键帧动画 转自:http://blog.csdn.net/zhuyingqingfen/article/details/12651017 /* 1.创建一个AnimManag ...

  7. 关于Nodejs的多进程模块Cluster

    关于Nodejs的多进程模块Cluster   前述 我们都知道nodejs最大的特点就是单进程.无阻塞运行,并且是异步事件驱动的.Nodejs的这些特性能够很好的解决一些问题,例如在服务器开发中,并 ...

  8. 使用css让XML文件按照HTML的风格显示出来

    attrib.css name { display:block; color:blue; font-size:20pt; font-weight:bold; } id,company,email,te ...

  9. TCP和UDP的聊天

    TCP聊天 TCP(Transmission Control Protocol,传输控制协议)是基于连接的协议. 1.一个TCP连接必须要经过三次"对话"才能建立起来,其中的过程非 ...

  10. LeetCode 刷题顺序表

    Id Question Difficulty Frequency Data Structures Algorithms 1 Two Sum 2 5 array + set sort + two poi ...