-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrEncryption.java
More file actions
58 lines (46 loc) · 1.48 KB
/
StrEncryption.java
File metadata and controls
58 lines (46 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.str.encryption;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StrEncryption {
public static void main(String[] args) {
calStrLength();
}
/**
* 控制台输入:time,me,ball字符串,通过程序可生成它的加密字符串 s=time#ball# ,对应的也可以生成索引indexs = {0,2,5},
* 逆向操作,通过字符串s和索引index可以转化出原字符串。计算输出最小的字符串长度?
* 例如:输入:time,me,ball,输出 10。
* 前提:0<strs<=2000,0< strs[i] < 7
*
*/
public static void calStrLength() {
Scanner scanner = new Scanner(System.in);
String restStr = "";
while(scanner.hasNext()) {
String line = scanner.nextLine();
String [] strs = line.split(",");
List<Integer> indexs = new ArrayList<>();
indexs.add(0);
//1.判断后一个字符串是否完全包含于前一个字符串
//2.主要添加基础条件
int index = 1;
restStr = strs[0] + "#";
while(index < strs.length) {
if(restStr.length() > strs[index].length()) {
int strIndex = restStr.indexOf(strs[index]);
if(strIndex < 0) {//表示不包含对应的字符串
indexs.add(restStr.length());
restStr = restStr + strs[index] + "#";
}else {
indexs.add(strIndex);
}
}
index++;
}
// System.out.println(indexs.toString());
System.out.println(restStr);
System.out.println(restStr.length());
}
scanner.close();
}
}