回答編集履歴

2

質問者さんのコードより辞書拝借差し替え

2018/08/31 10:19

投稿

opyon
opyon

スコア1009

test CHANGED
@@ -6,19 +6,23 @@
6
6
 
7
7
  (未検証)
8
8
 
9
-
10
-
11
- 全文字パターンとなるとArrayListが大量に必要になるので実用的ではないかも知れません。
12
-
13
- 入力や辞書をどう扱うかは好みで作り変えてください。
14
-
15
9
  取り急ぎ全体の流れの参考になればと思います。
16
10
 
17
11
 
18
12
 
13
+ 回答編集:
14
+
15
+ 辞書のみ質問者さんのコードより拝借し差し替えました。
16
+
17
+
18
+
19
19
  ###出力結果
20
20
 
21
- [shoubousha, syoubousya, shoubousha, syoubousya]
21
+ //しょうぼうしゃ
22
+
23
+ //[shoubousha, syoubousya, cilyouboucilya, cixyouboucixya, silyoubousilya, sixyoubousixya, shilyouboushalya, shixyouboushaxya, shoubousha, syoubousya, cilyouboucilya, cixyouboucixya, silyoubousilya, sixyoubousixya, shilyouboushalya, shixyouboushaxya, shoubousha, syoubousya, cilyouboucilya, cixyouboucixya, silyoubousilya, sixyoubousixya, shilyouboushalya, shixyouboushaxya, shoubousha, syoubousya, cilyouboucilya, cixyouboucixya, silyoubousilya, sixyoubousixya, shilyouboushalya, shixyouboushaxya, shoubousha, syoubousya, cilyouboucilya, cixyouboucixya, silyoubousilya, sixyoubousixya, shilyouboushalya, shixyouboushaxya, shoubousha, syoubousya, cilyouboucilya, cixyouboucixya, silyoubousilya, sixyoubousixya, shilyouboushalya, shixyouboushaxya, shoubousha, syoubousya, cilyouboucilya, cixyouboucixya, silyoubousilya, sixyoubousixya, shilyouboushalya, shixyouboushaxya, shoubousha, syoubousya, cilyouboucilya, cixyouboucixya, silyoubousilya, sixyoubousixya, shilyouboushalya, shixyouboushaxya]
24
+
25
+
22
26
 
23
27
 
24
28
 
@@ -28,8 +32,6 @@
28
32
 
29
33
  import java.util.HashMap;
30
34
 
31
- import java.util.List;
32
-
33
35
  import java.util.Map;
34
36
 
35
37
 
@@ -38,21 +40,13 @@
38
40
 
39
41
 
40
42
 
41
- // しょうぼうしゃ
42
-
43
- // [shoubousha, shoubousya, syoubousha, syoubousya]
44
-
45
-
46
-
47
43
  public static void main(String[] args) {
48
44
 
49
45
 
50
46
 
51
47
  //辞書作成
52
48
 
53
- //※変換に必要なケースだけ辞書登録てください
49
+ //辞書のみ質問者さんのコードより拝借差し替え
54
-
55
- //※このサンプルコードでは「しょうぼうしゃ」に必要な辞書しかありません。
56
50
 
57
51
  makeMap();
58
52
 
@@ -78,194 +72,158 @@
78
72
 
79
73
  }
80
74
 
81
-
82
-
83
- private static Map<String, List<String>> map = new HashMap<String, List<String>>();
84
-
85
-
86
-
87
- //しょ
88
-
89
- private static List<String> setSho = new ArrayList<String>();
90
-
91
- //う
92
-
93
- private static List<String> setU = new ArrayList<String>();
94
-
95
- //
96
-
97
- private static List<String> setBo = new ArrayList<String>();
98
-
99
- //しゃ
100
-
101
- private static List<String> setSha = new ArrayList<String>();
75
+
76
+
77
+ private static ArrayList<String> kanaToRoma(String kana) {
78
+
79
+ ArrayList<String> result = new ArrayList<String>();
80
+
81
+ ArrayList<String> tmp = new ArrayList<String>();
82
+
83
+ result.add("");
84
+
85
+
86
+
87
+ final int n = kana.length();
88
+
89
+ int t = 1;//通り
90
+
91
+
92
+
93
+ boolean is2Char = false;
94
+
95
+
96
+
97
+ for (int i = 0; i < n - 1; i++) {
98
+
99
+
100
+
101
+ //"しょ"などの2文字のものがmapにあるか確認
102
+
103
+ String key = kana.substring(i, i + 2);
104
+
105
+
106
+
107
+ if (map.containsKey(key)) {
108
+
109
+ is2Char= true;
110
+
111
+ }else {
112
+
113
+ is2Char = false;
114
+
115
+ key = kana.substring(i, i + 1);
116
+
117
+ }
118
+
119
+
120
+
121
+ //組み合わせの数だけ配列を増やす
122
+
123
+ //しゃ=sha,sya=2通り
124
+
125
+ //2通りなら2倍
126
+
127
+ int p = map.get(key).length;
128
+
129
+
130
+
131
+ t *= p;//全通り組み合わせ数
132
+
133
+
134
+
135
+ //加工用配列に前回までの処理内容を移す
136
+
137
+ tmp.clear();
138
+
139
+ tmp.addAll(result);
140
+
141
+
142
+
143
+ for (int x = 0; x < p - 1; x++) {
144
+
145
+ result.addAll(tmp);
146
+
147
+ }
148
+
149
+ tmp.clear();
150
+
151
+
152
+
153
+ int q = result.size();
154
+
155
+
156
+
157
+ String[] list = map.get(key);
158
+
159
+
160
+
161
+ int k = 0;
162
+
163
+ for (int j = 0; j < q; j++) {
164
+
165
+
166
+
167
+ tmp.add(result.get(j) + list[k]);
168
+
169
+ if (k == list.length - 1) {
170
+
171
+ k = 0;
172
+
173
+ } else {
174
+
175
+ k++;
176
+
177
+ }
178
+
179
+ }
180
+
181
+
182
+
183
+ //加工用配列から戻り値用配列へデータを移す
184
+
185
+ result.clear();
186
+
187
+ result.addAll(tmp);
188
+
189
+
190
+
191
+ //2文字処理時のみインクリメントを2文字分進める
192
+
193
+ if(is2Char) {
194
+
195
+ i++;
196
+
197
+ }
198
+
199
+ }
200
+
201
+ return result;
202
+
203
+ }
204
+
205
+
206
+
207
+ private static Map<String, String[]> map = new HashMap<>();
102
208
 
103
209
 
104
210
 
105
211
  private static void makeMap() {
106
212
 
107
-
108
-
109
- setSho.add("sho");
110
-
111
- setSho.add("syo");
112
-
113
- map.put("しょ", setSho);
114
-
115
-
116
-
117
- setU.add("u");
118
-
119
- map.put("う", setU);
213
+ map.put("う", new String[] {"u"});
120
-
121
-
122
-
214
+
123
- setBo.add("bo");
215
+ map.put("", new String[] {"si", "shi", "ci"});
124
-
216
+
125
- map.put("ぼ", setBo);
217
+ map.put("ぼ", new String[] {"bo"});
126
-
127
-
128
-
129
- setSha.add("sha");
218
+
130
-
131
- setSha.add("sya");
132
-
133
- map.put("しゃ", setSha);
219
+ map.put("しゃ", new String[] {"sha", "sya", "cilya" , "cixya", "silya", "sixya", "shalya", "shaxya"});
220
+
134
-
221
+ map.put("しょ", new String[] {"sho", "syo", "cilyo" , "cixyo", "silyo" , "sixyo", "shilyo" , "shixyo"});
135
-
136
222
 
137
223
  }
138
224
 
139
-
140
-
141
- private static ArrayList<String> kanaToRoma(String kana) {
142
-
143
- ArrayList<String> result = new ArrayList<String>();
144
-
145
- ArrayList<String> tmp = new ArrayList<String>();
146
-
147
- result.add("");
148
-
149
-
150
-
151
- final int n = kana.length();
152
-
153
- int t = 1;//通り
154
-
155
-
156
-
157
- boolean is2Char = false;
158
-
159
-
160
-
161
- for (int i = 0; i < n - 1; i++) {
162
-
163
-
164
-
165
- //"しょ"などの2文字のものがmapにあるか確認
166
-
167
- String key = kana.substring(i, i + 2);
168
-
169
-
170
-
171
- if (map.containsKey(key)) {
172
-
173
- is2Char= true;
174
-
175
- }else {
176
-
177
- is2Char = false;
178
-
179
- key = kana.substring(i, i + 1);
180
-
181
- }
182
-
183
-
184
-
185
- //組み合わせの数だけ配列を増やす
186
-
187
- //しゃ=sha,sya=2通り
188
-
189
- //2通りなら2倍
190
-
191
- int p = map.get(key).size();
192
-
193
-
194
-
195
- t *= p;//全通り組み合わせ数
196
-
197
-
198
-
199
- //加工用配列に前回までの処理内容を移す
200
-
201
- tmp.clear();
202
-
203
- tmp.addAll(result);
204
-
205
-
206
-
207
- for (int x = 0; x < p - 1; x++) {
208
-
209
- result.addAll(tmp);
210
-
211
- }
212
-
213
- tmp.clear();
214
-
215
-
216
-
217
- int q = result.size();
218
-
219
-
220
-
221
- List list = map.get(key);
222
-
223
-
224
-
225
- int k = 0;
226
-
227
- for (int j = 0; j < q; j++) {
228
-
229
-
230
-
231
- tmp.add(result.get(j) + list.get(k));
232
-
233
- if (k == list.size() - 1) {
234
-
235
- k = 0;
236
-
237
- } else {
238
-
239
- k++;
240
-
241
- }
242
-
243
- }
244
-
245
-
246
-
247
- //加工用配列から戻り値用配列へデータを移す
248
-
249
- result.clear();
250
-
251
- result.addAll(tmp);
252
-
253
-
254
-
255
- //2文字処理時のみインクリメントを2文字分進める
256
-
257
- if(is2Char) {
258
-
259
- i++;
260
-
261
- }
262
-
263
- }
264
-
265
- return result;
266
-
267
- }
268
-
269
225
  }
270
226
 
227
+
228
+
271
229
  ```

1

出力結果追加

2018/08/31 10:19

投稿

opyon
opyon

スコア1009

test CHANGED
@@ -10,12 +10,18 @@
10
10
 
11
11
  全文字パターンとなるとArrayListが大量に必要になるので実用的ではないかも知れません。
12
12
 
13
- 辞書をどう扱うかは好みで作り変えてください。
13
+ 入力や辞書をどう扱うかは好みで作り変えてください。
14
14
 
15
15
  取り急ぎ全体の流れの参考になればと思います。
16
16
 
17
17
 
18
18
 
19
+ ###出力結果
20
+
21
+ [shoubousha, syoubousya, shoubousha, syoubousya]
22
+
23
+
24
+
19
25
  ```java
20
26
 
21
27
  import java.util.ArrayList;