ユニークワードによって文字列圧縮:私はそうのようなロスレススタイルで最初のユニークワードのインデックスを使用して文字列を圧縮するために必要とされた問題持って
始まる文字列を:DUPLICATES を持つ任意の文圧縮後の出力:ユニークワードに関連する単語のリスト
自分でコードを作成しようとした後、解決策をオンラインで検索しました。私はそれのようなものを見つけることができませんでした。
ユニークワードによって文字列圧縮:私はそうのようなロスレススタイルで最初のユニークワードのインデックスを使用して文字列を圧縮するために必要とされた問題持って
始まる文字列を:DUPLICATES を持つ任意の文圧縮後の出力:ユニークワードに関連する単語のリスト
自分でコードを作成しようとした後、解決策をオンラインで検索しました。私はそれのようなものを見つけることができませんでした。
このようなデータ処理の質問については、Stream APIは非常に強力で簡潔です。
String words = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY";
// create a dictionary
Map<String, Integer> lookup = new LinkedHashMap<>();
// go through each word
String code = Stream.of(words.split(" "))
// lookup the code for that word, or add one as needed
.map(w -> lookup.computeIfAbsent(w, k -> lookup.size() + 1))
// turn the codes into Strings
.map(Object::toString)
// join them together as one String.
.collect(Collectors.joining(""));
System.out.println(code);
// dump the dictionary.
lookup.forEach((w, c) -> System.out.println(c + "=" + w));
プリント
12345678913967845
1=ASK
2=NOT
3=WHAT
4=YOUR
5=COUNTRY
6=CAN
7=DO
8=FOR
9=YOU
あなたは36ワード
String words = "Peter Piper picked a peck of pickled peppers. " +
"A peck of pickled peppers Peter Piper picked. " +
"If Peter Piper picked a peck of pickled peppers, " +
"Where's the peck of pickled peppers Peter Piper picked?";
Map<String, Integer> lookup = new LinkedHashMap<>();
String code = Stream.of(words.split("([.,?] *| +)"))
.map(w -> lookup.computeIfAbsent(w, k -> lookup.size() + 1))
.map(c -> Integer.toString(c, 36))
.collect(Collectors.joining(""));
System.out.println(code);
lookup.forEach((w, c) -> System.out.println(Integer.toString(c, 36) + "=" + w));
プリント
1234567895678123a12345678bc5678123
1=Peter
2=Piper
3=picked
4=a
5=peck
6=of
7=pickled
8=peppers
9=A
a=If
b=Where's
c=the
@KieranEvans実際、学生が自分の宿題として渡すことができる答えは、一般的には悪い考えです。少なくとも、それを行うための何かを学ばなければなりません。 –
"36 words"? –
@ cricket_007というコードは、最大36個のユニークワードと1文字コードで動作しますが、12個のユニークワードがあります。 –
これを行う簡単な方法は、問題の単語であるキーとマップインデックスである値でハッシュマップを定義することです。
Map<String, Integer> dictionary = new HashMap<>();
// Build the dictionary of strings
for(String word : arrWords) {
word = word.toUpperCase();
if (!dictionary.contains(word)) {
// Insert the word into the map.
dictionary.put(word, dictionary.size());
}
}
その後です。辞書がで印刷されません
for(String word : arrWords) {
print dictionary.get(word) + " ";
}
:あなたがマップでそれらを検索して、単語を印刷することができ、最後に
// Print the dictionary
for(Entry<String, Integer> entry : dictionary.entrySet()) {
String line = entry.getValue() + ":" + entry.getKey();
print it somewhere...
}
:あなただけの「キー」ファイルのマップをプリントアウトすることができます数字順。私はそれを理解するためにあなたを残すでしょう。
のために「その他の答えが正しいですが、あなたはドン場合はできるようにするには、この例を拡張することができます地図などを扱いたいあなたの問題へのより基本的なアプローチである:
String str = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY";
String[] words = str.split("\\s+"); // Create a string array of the words in the string by splitting them around whitespace
ArrayList<String> uniqueWords = new ArrayList<String>();
uniqueWords.add(words[0]);
String result = "1";
boolean thereAlready = false; // Flag to be set if a word is not unique
for (int i = 1; i < words.length; i++) { // Iterate through every word
thereAlready = false;
for (int j = 0; j < uniqueWords.size(); j++) { // Iterate through previously found words to see if it matches
if (words[i].equals(uniqueWords.get(j))) { // If the word is already there, modify the result string accordingly, set the flag, and break out of the inner loop
result += (j + 1);
thereAlready = true;
break;
}
}
if (!thereAlready) { // If the word is new, add it to the found words and modify the result string accordingly
uniqueWords.add(words[i]);
result += uniqueWords.size();
}
}
System.out.println(result);
出力:このため12345678913967845
おかげで、私もこれに苦しんでいました。
:P XD
歓声ATB、
ドウナット。
LinkedHashSetを使用すると、重複を削除して注文を保存することができます –
@ cricket_007これを実装する方法と場所はどこですか?私はちょうど1つを追加しようとしましたが、for()ループのためにこれをどのように使用しますか? – student1234