[LeetCode 471] Encode String with Shortest Length

Trick:  int r = (sub + sub).find(sub, 1); 寻找重复pattern。sub.size() % r == 0一定成立。否则设r是查找结果,总能找到更小的r满足条件。

 1 class Solution {
 2 public:
 3     string encode(string s) {
 4         int n = s.size();
 5         vector<vector<string>> dp(n + 1, vector<string>(n + 1));
 6         for (int i = 0; i < n; i++) {
 7             dp[i][i + 1].push_back(s[i]);
 8         }
 9         for (int k = 2; k <= n; k++) {
10             for (int i = 0, j = k; j <= n; i++, j++) {
11                 dp[i][j] = dp[i][j - 1] + dp[j - 1][j];
12                 for (int m = i + 1; m < j; m++) {
13                     string tmp = dp[i][m] + dp[m][j];
14                     if (tmp.size() < dp[i][j].size()) {
15                         dp[i][j] = tmp;
16                     }
17                 }
18                 int len = j - i;
19                 string sub = s.substr(i, len);
20                 int r = (sub + sub).find(sub, 1);
21                 if (r < len) {
22                     string tmp = to_string(len / r) + "[" + dp[i][i + r] + "]";
23                     if (tmp.size() < dp[i][j].size()) {
24                         dp[i][j] = tmp;
25                     }
26                 }
27             }
28         }
29         return dp[0][n];
30     }
31 };
时间: 2024-12-21 22:56:05

[LeetCode 471] Encode String with Shortest Length的相关文章

Leetcode: Encode String with Shortest Length &amp;&amp; G面经

Given a non-empty string, encode the string such that its encoded length is the shortest. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note: k will be a positive integ

[LeetCode] Encode String with Shortest Length 最短长度编码字符串

Given a non-empty string, encode the string such that its encoded length is the shortest. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note: k will be a positive integ

LeetCode题解 #8 String to Integer (atoi)

又是一道恶心的简单题. 一开始没想到这么多情况的,幸好LeetCode是个很人性化的oj,能让你知道你在哪个case上错了,否则一辈子都过不了. 考虑不周到只能一个个补了. 列举一下恶心的case //" 010" //" +004500" //" -0012a42" //"2147483648" //" b11228552307" //"18446744073709551617" //

[LeetCode#271] Encode and Decode Strings

Problem: Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Machine 1 (sender) has the function: string encode(vector<string> strs) { //

Bash String Manipulation Examples – Length, Substring, Find and Replace--reference

In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable with its value. This feature of shell is called parameter expansion. But parameter expansion has numerous other forms which allow you to expand a parame

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *

[LeetCode] 535. Encode and Decode TinyURL 编码和解码短URL

Note: This is a companion problem to the System Design problem: Design TinyURL.TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk

string 中的 length函数 和size函数 返回值问题

string 中的 length函数 和 size函数 的返回值  (  还有 char [ ] 中 测量字符串的  strlen 函数 ) 应该是 unsigned int 类型的 不可以 和 -1 比较. 应尽量避免 unsigned int 类型 和 int类型 数据 的比较 .当unsigned int 类型 和 int类型 数据 比较 时 ,会 把int 类型 转换 为 unsigned int类型 .如果 int是负数 ,转换 为 unsigned int 会是 一个 很大 的正整数

python——unpack问题 ocr_recognize timeout , exception:unpack requires a string argument of length 46

rObjBuff = "".join(unpack('=%ds' % ObjLen, recv_buf[6+i*ObjLen:6+(i+1)*ObjLen]))score, bbox1, bbox2, bbox3, bbox4, p00, p01, p10, p11, p20, p21, p30, p31, p40, p41 = unpack('=5f10H', rObjBuff) 错误示例: rObjBuff = unpack('=%ds' % ObjLen, recv_buf[6+