substring

Codeforces 873 B. Balanced Substring(前缀和 思维)

自作多情 提交于 2020-02-08 04:51:37
题目链接: Balanced Substring 题意:   求一个只有1和0的字符串中1与0个数相同的子串的最大长度。 题解:   我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个前缀和出现的最后位置。因为两个相同的前缀和之间的子串一定符合条件,最后只用遍历一次,将每个前缀与和这个前缀值相同的位置之间的长度求出来就是符合条件的子串长度。但是这里一定要注意!在整个子串未开始遍历的时候这个子串的前缀和也是0。我就是在这个地方错了,这里给出错地方的数据。 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAX_N = 1e5+9; 4 const int INF = 1e5; 5 char vec[MAX_N]; 6 int res[MAX_N],val[MAX_N*2]; 7 int N,M,t,num; 8 void init() 9 { 10 res[0] = 0; 11 memset(val,0,sizeof(val)); 12 } 13 int main() 14 { 15 cin>>N; 16 scanf("%s",vec+1); 17 for(int i=0;i<=N;i++) 18 { 19 if(vec[i] == '0') res[i+1] = res[i] - 1;

837B. Balanced Substring

一世执手 提交于 2020-02-08 04:51:24
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, 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.

[LeetCode] 76. Minimum Window Substring 解题思路

[亡魂溺海] 提交于 2020-02-08 04:40:07
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC" . Note: If there is no such window in S that covers all characters in T, return the empty string "" . If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S. 问题: 给定字符串 S 和 T ,求 S 中包含所有 T 中字符的最小子字符串。 值得一提的是, “包含所有 T 中的字符”,是要包含 T 中所有字符以及对应的个数 。在 T 中重复出现的字符,在最后答案中也需要包含相同个数的重复字符。第一次解没有理解到这个点,提出结果错误,意识到这个点后,修正并提交成功。 解题思路采用的也是 滑动窗口算法( Slide Window

Minimum Window Substring

梦想的初衷 提交于 2020-02-08 04:35:20
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC" . Note: If there is no such window in S that covers all characters in T, return the emtpy string "" . If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S. class Solution { private : int hashs[ 256 ]; int hasht[ 256 ]; inline bool check() { for ( int i= 0 ;i< 256 ;i++) if (hashs[i]<hasht[i]) return false ; return true ; } public : string

[LeetCode 1177] Can Make Palindrome from Substring

元气小坏坏 提交于 2020-02-08 04:32:51
Given a string s , we make queries on substrings of s . For each query queries[i] = [left, right, k] , we may rearrange the substring s[left], ..., s[right] , and then choose up to k of them to replace with any lowercase English letter. If the substring is possible to be a palindrome string after the operations above, the result of the query is true . Otherwise, the result is false . Return an array answer[] , where answer[i] is the result of the i -th query queries[i] . Note that: Each letter is counted individually for replacement so if for example s[left..right] = "aaa" , and k = 2 , we can

Combining substrings in C# with a custom format?

左心房为你撑大大i 提交于 2020-02-08 02:49:25
问题 Part of an app I'm creating in C# replaces certain substrings in a string with a value in square brackets like [11] . Often there can be the same value straight after - so I want to reduce the amount of text by combining them into one like [11,numberOfSame] For example, if the string contains: blahblah[122][122][122]blahblahblahblah[18][18][18][18]blahblahblah The desired new string would be: blahblah[122,3]blahblahblahblah[18,4]blahblahblah Would anyone know how I would do this? Thanks! :)

Leetcode 3 Longest Substring Without Repeating Characters. (最长无重复字符子串) (滑动窗口, 双指针)

邮差的信 提交于 2020-02-08 01:21:34
目录 问题描述 例子 方法 Leetcode 3 问题描述 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. 方法   保留一个将字符串中的字符存储为键并将其位置存储为值的hashmap,并保留两个定义最大子字符串的指针。移动右指针以浏览字符串,同时更新hashmap。如果字符已经在hashmap中,则将左指针移到最后找到的相同字符的右边

leetcode 3. Longest Substring Without Repeating Characters

元气小坏坏 提交于 2020-02-07 14:39:29
题目内容 Given a string, find the length of the longest substring without repeating characters. Example: 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. 分析过程 题目归类: array,遍历,dp 题目分析: 找到连续字串,输出最长的没有重复字母的连续字串。 采用循环法一定可以做出来,但是时间复杂度太差,最差是O(n^2),因为每次子串增长1都需要遍历一次看看有没有重复的数据,可以用一个hashmap来记录子串的数据。其中key为

Longest Substring with At Least K Repeating Characters

喜欢而已 提交于 2020-02-07 09:12:15
1. 解析 题目大意,求解最长的子序列,该子序列中不同字符出现的次数不少于k次。 2. 分析 按照我们正常的思路就是无非就是检测每个字符字串的组合,但存在一个问题是如果如何避免每次从开头的下一个字符开始遍历。就是设置一个mask掩码,字符串只由26个小写字母组成,最多用一个26位的二进制掩码即可表示,另外用一个计数器,记录每个字符出现的次数,若在子串当中的不同字符出现的次数都大于或等于k,那么掩码就会被重置为0,若该子串的长度大于之前的最大长度,用该子串更新当前的最大值。 并记录下当前的索引,这是关键,下一次就可以从该位置的下一个位置开始遍历,因为之前的状态已经检测过,防止每次都从头开始遍历。 时间复杂度为 例如:s = "ababbc" k = 2 'a', mask = 1 res = 0 max_index = 0 'b', mask = 11 res = 0 max_index = 0 'a', mask = 1 0 res = 0 max_index = 0 即若字串中的某个字符出现的次数大于或等于k,对应的mask位置就会被置为0 'b', mask = 0 0 res = 4 max_index = 3 'b', mask = 0 0 res = 5 max_index = 4 'c', mask = 100 res = 5 max_index = 4 class

Substring Frequency(KMP)

廉价感情. 提交于 2020-02-06 19:22:30
A string is a finite sequence of symbols that are chosen from an alphabet. In this problem you are given two non-empty strings A and B, both contain lower case English alphabets. You have to find the number of times B occurs as a substring of A. Input Input starts with an integer T (≤ 5), denoting the number of test cases. Each case starts with two lines. First line contains A and second line contains B. You can assume than 1 ≤ length(A), length(B) ≤ 106. Output For each case, print the case number and the number of times B occurs as a substring of A. Sample Input 4 axbyczd abc abcabcabcabc