首页 > 其他分享 >Maximum Deletions on a String

Maximum Deletions on a String

时间:2022-10-06 17:12:06浏览次数:97  
标签:Deletions letters String int Maximum equal next Delete first

Maximum Deletions on a String

You are given a string s consisting of only lowercase English letters. In one operation, you can:

  • Delete the entire string s , or
  • Delete the first i letters of s if the first i letters of s are equal to the following i letters in s , for any i in the range $1 \leq i \leq s.length / 2$.
  • For example, if  s = "ababc" , then in one operation, you could delete the first two letters of s to get  "abc" , since the first two letters of s and the following two letters of s are both equal to  "ab" .

Return the maximum number of operations needed to delete all of  s .

Example 1:

Input: s = "abcabcdabc"
Output: 2
Explanation:
- Delete the first 3 letters ("abc") since the next 3 letters are equal. Now, s = "abcdabc".
- Delete all the letters.
We used 2 operations so return 2. It can be proven that 2 is the maximum number of operations needed.
Note that in the second operation we cannot delete "abc" again because the next occurrence of "abc" does not happen in the next 3 letters.

Example 2:

Input: s = "aaabaab"
Output: 4
Explanation:
- Delete the first letter ("a") since the next letter is equal. Now, s = "aabaab".
- Delete the first 3 letters ("aab") since the next 3 letters are equal. Now, s = "aab".
- Delete the first letter ("a") since the next letter is equal. Now, s = "ab".
- Delete all the letters.
We used 4 operations so return 4. It can be proven that 4 is the maximum number of operations needed.

Example 3:

Input: s = "aaaaa"
Output: 5
Explanation: In each operation, we can delete the first letter of s.

 

解题思路

  看到数据范围考虑用dp。

  定义状态$f(i)$表示删除$[i,n]$这个字符串的所有操作方案的集合,属性是最大值。根据前缀的$j$个字符与后$j$个字符是否相同来划分集合,即如果$[i,i+j-1]$与$[i+j, i+2 \cdot j - 1]$相同,则可以从$f(i+j)$转移到$f(i)$。状态转移方程就是$f(i) = max \{ {f(i+j)+1} \}$,$j$要满足$j \leq \frac{n - i + 1}{2}$。

  这里判断同一个字符串中两个子串是否相同需要用到字符串哈希,如果用substr来判断的话会超时。一开始忘了这个模型,导致一直TLE。

  AC代码如下:

 1 typedef unsigned long long ULL;
 2 
 3 const int N = 4010, P = 131;
 4 
 5 ULL h[N], p[N];
 6 
 7 class Solution {
 8 public:
 9     ULL get(int l, int r) {
10         return h[r] - h[l - 1] * p[r - l + 1];
11     }
12     
13     int deleteString(string s) {
14         int n = s.size();
15         p[0] = 1;
16         for (int i = 1; i <= n; i++) {
17             h[i] = h[i - 1] * P + s[i - 1];
18             p[i] = p[i - 1] * P;
19         }
20         
21         vector<int> f(n + 2);
22         for (int i = n; i; i--) {
23             f[i] = 1;   // 全部删除的情况
24             for (int j = 1; i + j + j - 1 <= n; j++) {
25                 if (get(i, i + j - 1) == get(i + j, i + j + j - 1)) f[i] = max(f[i], f[i + j] + 1);
26             }
27         }
28         
29         return f[1];
30     }
31 };

 

参考资料

  力扣第313场周赛:https://www.bilibili.com/video/BV16g411a7t6/

标签:Deletions,letters,String,int,Maximum,equal,next,Delete,first
From: https://www.cnblogs.com/onlyblues/p/16758017.html

相关文章

  • 就因为JSON.stringify,我的年终奖差点打水漂了
    本文已参与 「掘力星计划」 ,赢取创作大礼包,挑战创作激励金。前言「欢迎在评论区讨论,掘金官方将在掘力星计划活动结束后,在评论区抽送100份掘金周边,抽奖详情见活动文章」......
  • java之String的一些常用方法
    string--字符串#######equals和==的区别?-equals:是比较两个对象是否一样(比较的内容->属性值)-==:比较两个地址是否一样-java8之前,常量池存放在堆中,java8以......
  • VC入门宝典三(String)
    CString何志丹主要内容:1,主要函数的实现2,常用函数3.CString与char[]的相互转换4,将NULL字节放入CString中vc中最主要函数不易理解。CString::CString(c......
  • VS2005 Debug版,dll /MTd,exe /MDd 跨dll使用CString的链接错误
    dll中导出函数DLL_EXPORTvoidDoString(CString&str);如果exe和dll都是/MD,一切正常如果dll/MTd,exe/MDd则找不到DoString,错误提示:errorLNK2019:无法解析的外部符......
  • 【笨方法学python】ex6 - 字符串(string)和文本
    代码如下:点击查看代码#-*-coding:utf-8--*-#字符串(string)和文本x="Thereare%dtypesofpeople."%10binary="bianry"do_not="don't"y="Thosewh......
  • string总结
    String函数总结string的函数,真香。(不总结迭代器的)------------##最基本的,头文件cpp<br/>#include<cstring><br/>#include<string><br/>就这两个含了string的,考试时......
  • 8.字符串容器string
    1.马上补充?.对于string中字母的大小写转换函数transform(str.begin(),str.end(),str.begin(),::tolower);//将str字符串中的大写转换为小写,保存在str中transform(st......
  • 002-Redis的 String 使用
    Redis命令十分丰富,包括的命令组有Cluster、Connection、Geo、Hashes、HyperLogLog、Keys、Lists、Pub/Sub、Scripting、Server、Sets、SortedSets、Strings、Transactions......
  • String和StringBuffer的选用
    String的字符串对象是字符串常量池里的,如果需要频繁的连接和改动的话,会导致很多不用的字符串常量被废弃此时,选用StringBuffer效率更高......
  • 1108 String复读机——20分
    给定一个长度不超过10^4的、仅由英文字母构成的字符串。请将字符重新调整顺序,按StringString....(注意区分大小写)这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一......