锁定老帖子 主题:给大家出道题,算法的
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (17)
|
|
---|---|
作者 | 正文 |
发表时间:2012-02-02
以前写过一个
http://shuidexiongdi.iteye.com/admin/blogs/1144179 |
|
返回顶楼 | |
发表时间:2012-02-02
若是C++的话做成k,v,可用下multimap
|
|
返回顶楼 | |
发表时间:2012-02-02
最后修改:2012-02-02
确实,素数不错
|
|
返回顶楼 | |
发表时间:2012-02-02
最后修改:2012-02-02
%w{hat top potter pot pier ripe}.group_by {|w| w.chars.sort.join }.each{|_,group| puts group.join(" ") } |
|
返回顶楼 | |
发表时间:2012-02-02
利用素数乘积唯一性
import java.util.HashMap; import java.util.Map; public class PrintGroupsWithSameLetters { private static final int[] prime = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 }; public static void main(String[] args) { String[] sample = { "hat", "top", "potter", "pot", "pier", "ripe" }; PrintGroupsWithSameLetters.PrintGroupsWithSameLetters(sample); } public static void PrintGroupsWithSameLetters(String[] words) { Map map = new HashMap(); for (int index = 0; index < words.length; index++) { PrintGroupsWithSameLetters.insertOrUpdate(map, words[index], getValue(words[index])); } System.out.println(map); } public static void insertOrUpdate(Map lib, String word, int value) { if (!lib.containsKey(value)) { lib.put(value, word); } else { lib.put(value, lib.get(value) + "," + word); } } public static int getValue(String word) { if (word == null || "".equals(word.trim())) { return -1; } word = word.toLowerCase(); int value = 1; char letter[] = word.toCharArray(); for (int index = 0; index < letter.length; index++) { int temp = letter[index] - 'a'; if (temp < 0 || temp > 25) { return -1; } value *= prime[temp]; } return value; } } |
|
返回顶楼 | |
发表时间:2012-02-02
seismosaurus 写道 利用素数乘积唯一性
import java.util.HashMap; import java.util.Map; public class PrintGroupsWithSameLetters { private static final int[] prime = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 }; public static void main(String[] args) { String[] sample = { "hat", "top", "potter", "pot", "pier", "ripe" }; PrintGroupsWithSameLetters.PrintGroupsWithSameLetters(sample); } public static void PrintGroupsWithSameLetters(String[] words) { Map map = new HashMap(); for (int index = 0; index < words.length; index++) { PrintGroupsWithSameLetters.insertOrUpdate(map, words[index], getValue(words[index])); } System.out.println(map); } public static void insertOrUpdate(Map lib, String word, int value) { if (!lib.containsKey(value)) { lib.put(value, word); } else { lib.put(value, lib.get(value) + "," + word); } } public static int getValue(String word) { if (word == null || "".equals(word.trim())) { return -1; } word = word.toLowerCase(); int value = 1; char letter[] = word.toCharArray(); for (int index = 0; index < letter.length; index++) { int temp = letter[index] - 'a'; if (temp < 0 || temp > 25) { return -1; } value *= prime[temp]; } return value; } } 这个很受用。 |
|
返回顶楼 | |
发表时间:2012-02-03
最后修改:2012-02-03
太简单了。。。
没你们想的那么复杂 |
|
返回顶楼 | |
发表时间:2012-02-03
最后修改:2012-02-03
不建议用素数乘积做,我之前也想过了这个问题
比如说一个单词 ZZZZZZ = (101)..101> 2的6次方*..... >2的36次方 想想就知道,这超过了int 的32位 从标准化来说,我也不知道这个值是多少,素数积是唯一那是在数学界,在计算机那就明显不一样,你打算用long么,要知道,超过long也是很简单的啊!上面才是6个字母 我想没必要把 over |
|
返回顶楼 | |
发表时间:2012-02-03
cucumber_pain 写道 不建议用素数乘积做,我之前也想过了这个问题
比如说一个单词 ZZZZZZ = (101)..101> 2的6次方*..... >2的36次方 想想就知道,这超过了int 的32位 从标准化来说,我也不知道这个值是多少,素数积是唯一那是在数学界,在计算机那就明显不一样,你打算用long么,要知道,超过long也是很简单的啊!上面才是6个字母 我想没必要把 over 用 BigInteger 呗 字符串要是比较长,不知道素数相乘的效果怎么样, 乘法还是很耗cpu资源的! 谁试试呗~ |
|
返回顶楼 | |
发表时间:2012-02-03
最后修改:2012-02-03
string Words[] = {"pot", "pier","ripe", "hat", "top", "potter"}; typedef multimap<string, string>::iterator MutiIter; int compare(const void *a, const void *b) { return (* (char*)a - * (char*)b); } void PrintGroupsWithSameLetters(string words[]) { multimap<string, string> map; for (int i = 0; i < 6; ++i) { char str[Words[i].length()]; strcpy(str, Words[i].c_str()); qsort(str, sizeof(str), sizeof(char), compare); string k(str); map.insert(pair<string, string>(k, Words[i])); } MutiIter iter; for (iter = map.begin(); iter != map.end(); ) { MutiIter end = map.upper_bound(iter->first); while (iter != end ) { cout << iter->second << " "; ++iter; } cout << endl; } } int main() { PrintGroupsWithSameLetters(Words); system("PAUSE"); return 0; } |
|
返回顶楼 | |