如何在ada编程中获得单词的字谜 . 例如:
我有一个字符串'one' . 怎么会混淆成'neo'或'eon'等?
示例代码:
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
WordText : String (1 .. 80);
Last : Natural;
begin
Put_Line("Enter Text: ");
Get_Line (WordText, Last);
-- example: I entered 'one'
-- it must be shuffle text per character
-- then it will print shuffled text: 'neo' or 'eno' or 'oen' etc.
Put_Line ("Text Shuffle: " &WordText (1 .. Last));
end Main;
1 回答
实现here和here描述的混杂算法之一 . 例如,
对于字典中的每个单词,对其连续字母进行排序,保留重复字符 . 使用Ada.Containers.Generic_Array_Sort的实例对字母进行排序 .
创建哈希映射;输入已排序的字符串作为 Map 's key; add the original word to the set of words that are permutatively equivalent, and use the set as the map'的值 . 使用Ada.Containers.Ordered_Sets的实例来保存单词集 . 使用Ada.Containers.Hashed_Maps的实例作为 Map .
对于给定的字符串,对其字母进行排序,查找映射集并打印其包含的单词 .
一个完整的例子是here .