动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String
2018-09-01 22:50:59
问题描述:

问题求解:
如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上。
后来发现是一个动态规划的问题,可以将每个字符结尾的最长长度进行保存,这样就巧妙的解决的重复的问题。
- The max number of unique substring ends with a letter equals to the length of max contiguous substring ends with that letter. Example
"abcd", the max number of unique substring ends with'd'is 4, apparently they are"abcd", "bcd", "cd" and "d".- If there are overlapping, we only need to consider the longest one because it covers all the possible substrings. Example:
"abcdbcd", the max number of unique substring ends with'd'is 4 and all substrings formed by the 2nd"bcd"part are covered in the 4 substrings already.- No matter how long is a contiguous substring in
p, it is inssinceshas infinite length.- Now we know the max number of unique substrings in
pends with'a', 'b', ..., 'z'and those substrings are all ins. Summary is the answer, according to the question.
public int findSubstringInWraproundString(String p) {
int res = 0;
int[] dp = new int[26];
int maxLen = 0;
for (int i = 0; i < p.length(); i++) {
if (i > 0 && (p.charAt(i) - p.charAt(i - 1) == 1 || p.charAt(i - 1) - p.charAt(i) == 25)) {
maxLen++;
}
else maxLen = 1;
dp[p.charAt(i) - 'a'] = Math.max(dp[p.charAt(i) - 'a'], maxLen);
}
for (int i = 0; i < 26; i++) res += dp[i];
return res;
}
动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String的更多相关文章
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- [Swift]LeetCode467. 环绕字符串中唯一的子字符串 | Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
- Leetcode: Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- LeetCode 467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 【LeetCode】467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串
详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...
- 467. [leetcode] Unique Substrings in Wraparound String
467. Unique Substrings in Wraparound String Implement atoi to convert a string to an integer. Hint: ...
随机推荐
- spring 配置文件属性设置默认值以及读取环境变量值
在 Spring 中为 javabean 注入属性文件中的属性值一般人都知道的,可以通过 org.springframework.beans.factory.config.PropertyPlaceh ...
- build custom centos7
必读,在以下内容之前. pre: 0. install log redhat6 mak iso guide redhat7 make iso guide 1. Linux安装>(三)发行版制作 ...
- 【TensorFlow】tf.nn.max_pool实现池化操作
max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似 有些地方可以从卷积去参考[TensorFlow]tf.nn.conv2d是怎样实现卷积的? tf.nn.max_pool(va ...
- Codeforces 700B Connecting Universities - 贪心
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's poss ...
- 【python017--函数对象1】
一.函数 1.定义函数:def 函数名(): 2.调用函数:直接写函数的名称() >>> def MyFirstFunction(): print('this my firs ...
- Bootstrap3基础 glyphicon 设置图标的颜色与大小
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- Python3 tkinter基础 Button command 单击按钮 在console中打印文本
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- MongoDB ReplicaSet 集群搭建
说明 本文创建的集群的名字为test,在同一台机器上创建了三个mongo实例,端口不同即可. 安装mongodb的教程,之前总结过,请参考:CentOS安装MongoDB笔记 创建实例 # 本机默认原 ...
- 【POJ1961】period
[POJ1961]period 题目描述 如果一个字符串S是由一个字符串T重复K次构成的,则称T是S的循环元.使K出现最大的字符串T称为S的最小循环元,此时的K称为最大循环次数. 现在给定一个长度为N ...
- 旅行商问题【山财新生赛E】
链接:https://ac.nowcoder.com/acm/contest/547/E 来源:牛客网 题目描述 旅行商来到了一个新的国家,这个国家有N个城市,他们直接由N-1条道路相连接,每条道路的 ...