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. UvaLive 6667 Longest Chain (分治求三元组LIS&amp;amp;树状数组)

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

  3. Two Sigma OA

    刚做了两道Two Sigma OA. 还是两道老题, Friend Cycle和Longest Chain. Friend Cycle可以用Union Find来做.优化的时候因为矩阵是沿对角线对称, ...

  4. (Problem 14)Longest Collatz sequence

    The following iterative sequence is defined for the set of positive integers: n n/2 (n is even) n 3n ...

  5. [LeetCode] Maximum Length of Pair Chain 链对的最大长度

    You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...

  6. 比特币_Bitcoin 简介

    2008-11   Satoshi Nakamoto  Bitcoin: A Peer-to-Peer Electronic Cash System http://p2pbucks.com/?p=99 ...

  7. Mix and Build(简单DP)

    Mix and Build Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3936 Accepted: 1203 Case Ti ...

  8. (Problem 74)Digit factorial chains

    The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...

  9. projecteuler----&amp;gt;problem=14----Longest Collatz sequence

    title: The following iterative sequence is defined for the set of positive integers: n n/2 (n is eve ...

随机推荐

  1. 《3D Math Primer for Graphics and Game Development》读书笔记2

    <3D Math Primer for Graphics and Game Development>读书笔记2 上一篇得到了"矩阵等价于变换后的基向量"这一结论. 本篇 ...

  2. WPF依赖属性DependencyProperty

    写在之前: 依赖属性算是WPF醉醉基础的一个组成了.平时写代码的时候,简单的绑定很轻松,但是遇到复杂的层次比较多的绑定,真的是要命.所以,我觉得深刻认识依赖属性是很有必要的.本篇只是个人学习的记录,学 ...

  3. Eclipse创建Android模拟器创建选项解释

  4. 解剖SQLSERVER 第十七篇 使用 OrcaMDF Corruptor 故意损坏数据库(译)

    解剖SQLSERVER 第十七篇 使用 OrcaMDF Corruptor 故意损坏数据库(译) http://improve.dk/corrupting-databases-purpose-usin ...

  5. Autofac Container 的简单的封装重构

    为了使用方便,对Autofac container的简单封装,记录如下,备以后用或分享给大家,欢迎讨论! using Autofac; using Autofac.Core.Lifetime; usi ...

  6. DSP using MATLAB 示例 Example3.12

    用到的性质 代码: n = -5:10; x = sin(pi*n/2); k = -100:100; w = (pi/100)*k; % freqency between -pi and +pi , ...

  7. 动态添加Marquee标签,并动态赋值与属性

    前台加载js $(function(){ var publishStr="<%=publishText%>" var marqueeStr=" <mar ...

  8. 记一次hadoop datanode进程问题分析

    症状:datanode进程还在,但是在web ui接口发现该节点已经被置为dead节点.监测datanode进程日志,开始时一直狂刷很忙,后来停止刷新日志. 分析datanode进程日志,发现如下一些 ...

  9. Java学习之路(六)

    1:包及和访问权限 将类放置到一个包当中,需要使用package “包名” 编译时需要使用 -d 参数  该参数的作用是依照包名生成相应的文件夹 一个类的全民应该是  “包名” + “.” + “类名 ...

  10. POJ 2396 Budget【网络流】

    题意: cas           //测试数据组数 n m         //行数 列数 a1 a2 ... an    //每行的和 b1 b2 ... bn   //每列的和 q       ...