Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- man car kile arc none like
- man
- car arc
- kile like
- none
- string line = Console.ReadLine();
- string []words=line.Split(' ');
- int[] numbers = GetUniqueInts(words);
- for (int i = 0; i < words.Length; i++)
- {
- if (table.ContainsKey(numbers[i]))
- {
- table[numbers[i]] = table[numbers[i]].Append(words[i]);
- }
- else
- {
- table.Add(numbers[i],new StringBuilder(words[i]));
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class FindAnagrams
- {
- static void Main(string[] args)
- {
- var lookup = args.ToLookup(word => SortLetters(word));
- foreach (var entry in lookup)
- {
- foreach (var word in entry)
- {
- Console.Write(word);
- Console.Write(" ");
- }
- Console.WriteLine();
- }
- }
- static string SortLetters(string original)
- {
- char[] letters = original.ToCharArray();
- Array.Sort(letters);
- return new string(letters);
- }
- }
- c:UsersJonTest>FindAnagrams.exe man car kile arc none like
- man
- car arc
- kile like
- none
- primes = [2, 41, 37, 47, 3, 67, 71, 23, 5, 101, 61, 17, 19, 13, 31, 43, 97, 29, 11, 7, 73, 83, 79, 89, 59, 53]
- def get_frequency_map(word):
- map = {}
- for letter in word:
- map[letter] = map.get(letter, 0) + 1
- return map
- def hash(word):
- map = get_frequency_map(word)
- product = 1
- for letter in map.iterkeys():
- product = product * primes[ord(letter)-97] ** map.get(letter, 0)
- return product
- from collections import defaultdict
- res = defaultdict(list)
- L = "car, acr, bat, tab, get, cat".split(", ")
- for w in L:
- res["".join(sorted(w))].append(w)
- print(res.values())
- unsigned char letter_frequency[26];
- def processword(word, downcase)
- word.chomp!
- word.squeeze!(" ")
- word.chomp!(" ")
- if (downcase)
- word.downcase!
- end
- if ($dict[word]==nil)
- stdword=word.downcase
- signature=$letters.collect {|letter| stdword.count(letter)}
- signature.each do |cnt|
- if (cnt>9)
- puts "Signature overflow:#{word}|#{signature}|#{cnt}"
- end
- end
- $dict[word]=[$wordid,signature]
- $wordid=$wordid+1
- end
- end
- #define BUCKETS 49999
- struct bucket {
- char *word;
- struct bucket *next;
- };
- static struct bucket hash_table[BUCKETS];
- static unsigned int hash_word(char *word)
- {
- char *p = word;
- unsigned int hash = 0;
- while (*p) {
- if (*p < 97 || *p > 122) {
- return 0;
- }
- hash |= 2 << (*p - 97);
- *p++;
- }
- return hash % BUCKETS;
- }
- public static HashMap<String, Integer> getHashMap(String word) {
- HashMap<String, Integer> map = new HashMap<String, Integer>();
- String[] chars = word.split("");
- int index = 0;
- for (String c : chars) {
- map.put(c, index);
- index++;
- }
- return map;
- }
- public static int alphaHash(String word, int base,
- HashMap<String, Integer> map) {
- String[] chars = word.split("");
- int result = 0;
- for (String c : chars) {
- if (c.length() <= 0 || c.equals(null)) {
- continue;
- }
- int index = 0;
- if (map.containsKey(c)) {
- index = map.get(c);
- } else {
- index = Integer.MAX_VALUE;
- }
- result += index;
- if (result > base) {
- return result;
- }
- }
- return result;
- }
- HashMap<String, Integer> map = getHashMap(sample);
- int sampleHash = alphaHash(sample, Integer.MAX_VALUE, map);
- for (String s : args) {
- if (sampleHash == alphaHash(s, sampleHash, map)) {
- System.out.print(s + " ");
- }
- }
- // structured for clarity
- static bool isAnagram(String s1, String s2)
- {
- int[] histogram = new int[256];
- int uniques = 0;
- // scan first string
- foreach (int c in s1)
- {
- // count occurrence
- int count = ++histogram[c];
- // count uniques
- if (count == 1)
- {
- ++uniques;
- }
- }
- // scan second string
- foreach (int c in s2)
- {
- // reverse count occurrence
- int count = --histogram[c];
- // reverse count uniques
- if (count == 0)
- {
- --uniques;
- }
- else if (count < 0) // trivial reject of longer strings or more occurrences
- {
- return false;
- }
- }
- // final histogram unique count should be 0
- return (uniques == 0);
- }
Add Comment
Please, Sign In to add comment