Amazon Campus(2013-Sep-24)Question 2 / 2 (Amazon Campus(17):Find the differences of items in amazon)
来源:互联网 发布:铝塑板折边工具淘宝 编辑:程序博客网 时间:2024/06/11 06:18
Amazon has millions of different items in different categories right now, so when sellers want to sell items in our website, sellers want to find the right categories their items belong to. Suppose we want to build a system to help sellers find the minimal differences items and then find the right category. The difference index is a number that sum of single-word edits (insertion, deletion, substitution) required to change one phrase into the other:
For example, we get two lines from standard input Hadoop in practice
Hadoop operations
The difference index of ‘Hadoop in practice’ and ‘Hadoop operations’ is 2. Because we can remove ‘practice’ and substitute ‘in’ with ‘operations’, then ‘Hadoop in practice’ can convert to ‘Hadoop operations’
For example, we get two lines from standard input
Hadoop cookbook
Hadoop operations
The difference index of ‘Hadoop cookbook’ and ‘Hadoop operations’ is 1. Because we can substitute ‘cookbook’ with ‘operations’ then convert 'Hadoop cookbook' can convert to 'Hadoop operations'
For example, we get two lines from standard input:
Ruby in action
Hadoop operations
The difference index of ‘Ruby in action’ and ‘Hadoop operations’ is 3. Because we can substitute ‘Ruby’ with ‘Hadoop’, ‘in’ with ‘operations’ and remove ‘action’ then 'Ruby in action' can convert to 'Hadoop operations'
测试案例:"Ruby in action","Hadoop operations" ----3"Hadoop in practice","Hadoop operations" ----2"Hadoop cookbook","Hadoop operations" ----1"Kindle Fire HD Tablet","Kindle Fire HD 8.9\" 4G LTE Wireless Tablet" ----4首先查找二者匹配的单词数maxMatch,再用二者单词数较多的减去maxMatch即可。因为不匹配的单词采用insertion,deletion或substitution中的某种方式一步即可完成。代码如下:
static int differ(String string1, String string2) { int res = 0; int maxMatch = 0; if(string1.equals(string2)) return res; String []words1 = string1.split(" "); String []words2 = string2.split(" "); List<String> words1List = Arrays.asList(words1); List<String> words2List = Arrays.asList(words2); for(int k=0; k<words2List.size(); k++) { int match = 0; for(int i=k; i<words2List.size(); i++) { String word2 = words2List.get(i); for(int j=0; j<words1List.size(); j++) { String word1 = words1List.get(j); if(word1.equals(word2)) { match++; break; } else { } } } if(match > maxMatch) maxMatch = match; } if(words1List.size() > words2List.size()) { res += words1List.size() - maxMatch; } else { res += words2List.size() - maxMatch; } return res; }
内牛满面,其实是最长公共子序列问题,采用DP的方法去百度之。
- Amazon Campus(2013-Sep-24)Question 2 / 2 (Amazon Campus(17):Find the differences of items in amazon)
- Amazon Campus(2013-Sep-24)Question 1 / 2 (Amazon Campus(15): Clock wise walk)
- Amazon Campus(2013-Sep-22)Question 2 / 2 (Amazon Campus(9): MM-Chess)
- Amazon Campus(2013-Sep-22)Question 1 / 2 (Amazon Campus(5): completely inside interval)
- Amazon Campus(2013-Sep-25)
- Amazon Hiring Campus 2013
- 亚马逊在线技术笔试(Amazon Hiring Campus 2013 - Final 6)
- Amazon
- Amazon云产品介绍(2) - Amazon SimpleDB
- [Amazon] Find loop in a singly linkedlist
- Code interview of Amazon
- Amazon 發表Kindle 2電子書閱讀器
- Amazon面经2016/2
- Amazon Simple Queue Service(Amazon SQS)
- [Amazon] Second Max of Array
- elastic ip in amazon ec2
- Amazon Dynamo: The Next Generation Of Virtual Distributed Storage
- Why Amazon Echo is the future of every home
- 二进制
- UVa 79 ClockHands (water ver.)
- nyoj-660-逃离地球
- js跨浏览器将字符串转化为xml对象
- 测试流程规范
- Amazon Campus(2013-Sep-24)Question 2 / 2 (Amazon Campus(17):Find the differences of items in amazon)
- 用递归求最大公约数
- poj 2942 (点双联通+判断二分图)
- scanf()函数的用法和实践
- PHP验证码图片在各类平台中的应用
- VMware’s Strategy for Software-Defined Storage
- json 简介
- 99乘法表打印
- Oracle索引分类