123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- import sys
- def is_prime(n):
- if n == 1:
- return False
- limit = int(n ** 0.5) + 1
- div = 2
- while div < limit:
- if n % div == 0:
- return False
- div += 1
- return True
- def hash_word(s, size):
- hash_idx = 0
- for j in range(len(s)):
- letter = ord(s[j]) - 96
- hash_idx = (hash_idx * 26 + letter) % size
- return hash_idx
- def step_size(s, const):
- return const - (hash_word(s, const))
- def insert_word(s, hash_table):
-
- word_idx = hash_word(s, len(hash_table))
-
- if hash_table[word_idx] == '':
-
- hash_table[word_idx] = s
- else:
-
- step_val = step_size(s, 3)
-
- while hash_table[word_idx] != '':
- word_idx = (word_idx + step_val) % len(hash_table)
-
- hash_table[word_idx] = s
- def find_word(s, hash_table):
- word_idx = hash_word(s, len(hash_table))
- tracking_idx = word_idx
- if hash_table[tracking_idx] == s:
- return True
- else:
-
- step_val = step_size(s, 1)
- count = 0
-
- while tracking_idx < len(hash_table) and hash_table[tracking_idx] != s and count < 3:
- tracking_idx = (tracking_idx + step_val) % (len(hash_table))
- if tracking_idx == word_idx:
- return False
- count += 1
- if tracking_idx >= len(hash_table):
- return False
-
- return s == hash_table[tracking_idx]
- def is_reducible(s, hash_table, hash_memo):
-
-
- if s == 'a' or s == 'i' or s == 'o':
- return True
-
-
- elif find_word(s, hash_memo):
- return True
-
-
- elif 'a' in s or 'i' in s or 'o' in s:
-
- if find_word(s, hash_table) is False:
- return False
-
- for i in range(len(s)):
- new_word = s[:i] + s[i + 1:]
-
- if is_reducible(new_word, hash_table, hash_memo):
- insert_word(new_word, hash_memo)
- return True
-
- else:
- return False
- def get_longest_words(string_list):
- max_word_length = 0
-
- for word in string_list:
- if len(word) > max_word_length:
- max_word_length = len(word)
-
- max_word_list = []
- for word in string_list:
- if len(word) == max_word_length:
- max_word_list.append(word)
-
- max_word_list.sort()
- return max_word_list
- def main():
-
- word_list = []
-
- for line in sys.stdin:
- line = line.strip()
- word_list.append(line)
-
- length_words = len(word_list)
-
-
- prime_num = (length_words * 2) + 1
- while is_prime(prime_num) is False:
- prime_num += 1
-
- hash_table = []
-
- for i in range(prime_num):
- hash_table.append('')
-
-
- for word in word_list:
- insert_word(word, hash_table)
-
-
-
-
-
- hash_memo = []
- hash_memo_size = int((len(word_list) * 0.2) + 1)
- while is_prime(hash_memo_size) is False:
- hash_memo_size += 1
-
- for i in range(hash_memo_size):
- hash_memo.append('')
-
- reducible_word_list = []
-
-
-
-
-
-
- for word in word_list:
- if is_reducible(word, hash_table, hash_memo):
- reducible_word_list.append(word)
-
- reducible_word_largest = get_longest_words(reducible_word_list)
-
-
- for word in reducible_word_largest:
- print(word)
- if __name__ == "__main__":
- main()
|