substring

Longest Substring Without Repeating Characters

杀马特。学长 韩版系。学妹 提交于 2020-02-12 00:33:09
package cn.edu.xidian.sselab.hashtable; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /** * * @author zhiyong wang * title: Longest Substring Without Repeating Characters * content: * Given a string, find the length of the longest substring without repeating characters. * For example, the longest substring without repeating letters for "abcabcbb" is "abc", * which the length is 3. * For "bbbbb" the longest substring is "b", with the length of 1 * */ public class LongestSubString { //这种方法,时间超时,如果字符串非常长,时间复杂度为O(n^2),肯定会超时,因为这是一个最优化问题,可以用动态规划来解

Longest Substring Without Repeating Characters

放肆的年华 提交于 2020-02-12 00:29:27
题意为给出一个字符串,找出其中没有重复字符的最长子序列的长度。brute force的复杂度为O(n^3).依次查找每个子字符串是否含有重复字符,并比较长度。开始看到题目,想用DP解决,在已有目前最长子序列的情况下,比较把当前字符串放入和不放入,哪个子序列长度会更大,但是这种解法的复杂度为O(n^2),也不理想。 一种比较好的解法是贪心策略,维护一个变量maxlen保存目前已有的不重复子序列的最大长度。维护一个sliding window,left为左起点,right为右端点。同时维护一个map,保存窗口中的<char,index>键值对。每次右端点向右移动一格,如果 s[right]不在map中,则窗口中的字符串长度加1,否则说明窗口中已经有和s[right]的重复的元素,要更新left的值,left的值为重复元素的后一个值,此时也要删去重复元素之前的在窗口中的值,对map做更新,这种思路的代码如下: class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ length = len(s) left=maxlen = 0 map = {} for right in xrange(length): if map.has_key(s[right]):

3. Longest Substring Without Repeating Characters

≡放荡痞女 提交于 2020-02-12 00:26:27
Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.最长不含重复字符的子字符串C++: 1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s) { 4 int curLen = 0 ; 5 int maxLen = 0 ; 6 vector<int>

3. Longest Substring Without Repeating Characters

北城余情 提交于 2020-02-11 23:28:24
暴力 class Solution { public int lengthOfLongestSubstring(String s) { int the_max = 0; HashSet<Character> hs = new HashSet<Character>(); for (int i = 0; i < s.length(); i++) { hs.clear(); int y = i; while ( y < s.length() ) { if ( hs.contains(s.charAt(y))) break; else { hs.add(s.charAt(y)); y++; } } the_max = Math.max(hs.size(), the_max); hs.clear(); } return the_max; } } 第二种解法 双指针做法 class Solution { public int lengthOfLongestSubstring(String s) { int the_max = 0; HashSet<Character> hs = new HashSet<Character>(); for(int i = 0,j =0 ; i < s.length() ; i++) { while(hs.contains(s.charAt(i))) { hs

小写金额转大写金额

余生长醉 提交于 2020-02-11 19:53:59
网上找到的,有小BUG,但写的不错. 亿级数据,尾数为00000的转换会转错,其它还没发现有问题. public static string CmycurD(decimal num) { string str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字 string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字 string str3 = ""; //从原num值中取出的值 string str4 = ""; //数字的字符串形式 string str5 = ""; //人民币大写金额形式 int i; //循环变量 int j; //num的值乘以100的字符串长度 string ch1 = ""; //数字的汉语读法 string ch2 = ""; //数字位的汉字读法 int nzero = 0; //用来计算连续的零值是几个 int temp; //从原num值中取出的值 num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数 str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式 j = str4.Length; //找出最高位 if (j > 15) { return "溢出"; } str2 = str2

CF1015F Bracket Substring(动态规划)

天涯浪子 提交于 2020-02-11 15:46:18
CF1015F Bracket Substring 这题一眼 GT考试 题意: 求有多少种合法的括号序列要求字符串S是其字串 数据范围大概是一个 \(\Theta(n^3)\) 的算法 从前往后填括号, 设 \(F[i][j][k]\) 表示填到第 \(i\) 位, "和"为 \(j\) , 匹配了 \(S k\) 位, 其中和的定义为将左括号看做1, 右括号看作-1, 一个合法的括号序列的前缀和总大于等于零且本身的和为零(一个左括号一定匹配一个右括号) \(dp\) 时只需枚举下一位填 \('('\) 或 \(')'\) 即可, 但要注意的是匹配 \(S\) 串时从第 \(k\) 位不匹配不是直接重新从0开始匹配而是求出它的 \(kmp\) 数组, 找到上一个 \(border\) , 可以参考代码 #pragma GCC optimize(2) #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (;!isdigit(c);c

codeforce #460 div2 D. Substring

瘦欲@ 提交于 2020-02-09 22:15:41
D. Substring time limit per test 3 seconds You are given a graph with n nodes and m directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are "abaca", then the value of that path is 3. Your task is find a path whose value is the largest. Input The first line contains two positive integers n, m (1 ≤ n, m ≤ 300 000), denoting that the graph has n nodes and m directed edges. The second line contains a string s with only lowercase English letters. The i-th character is the

年月日时分的时间差函数,统计两个时间的差值

痞子三分冷 提交于 2020-02-09 12:47:14
//计算时间差函数 difference ( ) { if ( this . formData . startTime !== '' && this . formData . endTime !== '' ) { let start = this . formData . startTime ; let end = this . formData . endTime ; console . log ( start , end , 88888 ) let dateBegin = new Date ( start . substring ( 0 , 10 ) ) ; let dateEnd = new Date ( end . substring ( 0 , 10 ) ) ; let dateDiff = dateEnd . getTime ( ) - dateBegin . getTime ( ) ; //时间差的毫秒数 let dayDiff = Math . floor ( dateDiff / ( 24 * 3600 * 1000 ) ) + 1 ; //计算出相差天数 if ( end . substring ( 11 , 13 ) < this . flyTime ) { //this.timeNode从字典表里面取,飞机起飞时间15时 dayDiff = dayDiff

Balanced Substring

谁说胖子不能爱 提交于 2020-02-08 05:43:04
You are given a string s consisting only of characters 0 and 1. A substring [ l ,  r ] of s is a string s l s l  + 1 s l  + 2 ... s r , and its length equals to r  -  l  + 1. A substring is called balanced if the number of zeroes ( 0) equals to the number of ones in this substring. You have to determine the length of the longest balanced substring of s . Input The first line contains n ( 1 ≤  n  ≤ 100000) — the number of characters in s . The second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s . Output If there is no non-empty balanced

【Cf edu 30 B. Balanced Substring】

雨燕双飞 提交于 2020-02-08 04:53:15
time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a string s consisting only of characters 0 and 1. A substring [ l ,  r ] of s is a string s l s l  + 1 s l  + 2 ... s r , and its length equals to r  -  l  + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring. You have to determine the length of the longest balanced substring of s . Input The first line contains n (1 ≤  n  ≤ 100000) — the number of characters in s . The second line contains a string s consisting of