substring

算法(二十)

微笑、不失礼 提交于 2020-01-31 23:51:55
1、给定一个字符串s和一组单词dict,判断s是否可以用空格分割成一个单词序列,使得单词序列中所有的单词都是dict中的单词(序列可以包含一个或多个单词)。 例如: 给定s=“leetcode”; dict=["leet", "code"]. 返回true,因为"leetcode"可以被分割成"leet code". 参考代码如下: public boolean wordBreak(String s, Set<String> dict) { if(s==null || s.length()==0 || dict == null || dict.size() == 0){ return false; } boolean[] flag = new boolean[s.length() + 1]; flag[0] = true; for(int i=1; i<=s.length();i++){ for(int j=i-1; j>=0; j--){ if(flag[j] && dict.contains(s.substring(j,i))){ flag[i] = true; break; }else{ flag[i] = false; } } } return flag[s.length()]; } 2、给定一个字符串s和一组单词dict,在s中添加空格将s变成一个句子

Number of distinct palindromic substrings

亡梦爱人 提交于 2020-01-31 18:01:52
问题 Given a string, I know how to find the number of palindromic substrings in linear time using Manacher's algorithm. But now I need to find the number of distinct/unique palindromic substrings. Now, this might lead to an O(n + n^2) algorithm - one 'n' for finding all such substrings, and n^2 for comparing each of these substrings with the ones already found, to check if it is unique. I am sure there is an algorithm with better complexity. I was thinking of maybe trying my luck with suffix trees

【前端学习笔记day64】9.12. jaavscript字符串处理方法

依然范特西╮ 提交于 2020-01-31 11:08:18
文章目录 9.12. 字符串处理方法 字符串处理方法 9.12. 字符串处理方法 字符串处理方法 1、字符串合并操作:“ + ” var iNum01 = 12; var iNum02 = 24; var sNum03 = '12'; var sTr = 'abc'; alert(iNum01+iNum02); //弹出36 alert(iNum01+sNum03); //弹出1212 数字和字符串相加等同于字符串相加 alert(sNum03+sTr); // 弹出12abc 2、parseInt() 将数字字符串转化为整数 var sNum01 = '12'; var sNum02 = '24'; var sNum03 = '12.32'; alert(sNum01+sNum02); //弹出1224 alert(parseInt(sNum01)+parseInt(sNum02)) //弹出36 alert(sNum03) //弹出数字12 将字符串小数转化为数字整数 3、parseFloat() 将数字字符串转化为小数 var sNum03 = '12.32' alert(parseFloat(sNum03)); //弹出 12.32 将字符串小数转化为数字小数 4、split() 把一个字符串分隔成字符串组成的数组 var sTr = '2017-4-22'; var

Problem with nested FOR-Loop and IF-Condition

混江龙づ霸主 提交于 2020-01-30 11:17:43
问题 I have some lines of text. Then I have a list with test-words. I like to look up each line of the text and check if one of the test-words appears in it. Beforehand this works well with a commands like these: IF not "!stringToTest:%searchstring%=!"=="!stringToTest!" However, now this seems to be more complicated as I have nested Loops? I try to create a little MWE for my problem: @echo off setlocal enabledelayedexpansion set /a counterPC=0 set "listPC=Win10,Motherboard,USB-Port,Core" FOR %%G

substring out of range

限于喜欢 提交于 2020-01-30 10:36:09
问题 I am trying to extract the number out of the last part of the string, I have wrote a function to do this but am having problems with out of range index. Here is the string type="value" cat=".1.3.6.1.4.1.26928.1.1.1.2.1.2.1.1" descCat=".1.3.6.1.4.1.26928.1.1.1.2.1.2.1.3" and here is my function private static string ExtractDescOID(string property) { string result = ""; int startPos = property.LastIndexOf("descOid=\"") + "descOid=\"".Length; int endPos = property.Length - 1; if (endPos -

substring out of range

杀马特。学长 韩版系。学妹 提交于 2020-01-30 10:35:08
问题 I am trying to extract the number out of the last part of the string, I have wrote a function to do this but am having problems with out of range index. Here is the string type="value" cat=".1.3.6.1.4.1.26928.1.1.1.2.1.2.1.1" descCat=".1.3.6.1.4.1.26928.1.1.1.2.1.2.1.3" and here is my function private static string ExtractDescOID(string property) { string result = ""; int startPos = property.LastIndexOf("descOid=\"") + "descOid=\"".Length; int endPos = property.Length - 1; if (endPos -

LeetCode:Longest Palindromic Substring

风格不统一 提交于 2020-01-28 19:31:23
暴力解法,果不其然超时 暴力解法程序(c++) bool isShorter ( const string & s1 , const string & s2 ) //自定义按照字符串大小降序排序 { return s1 . size ( ) > s2 . size ( ) ; } class Solution { public : string longestPalindrome ( string s ) { if ( s . empty ( ) ) //检验字符串是否为空,否则报错 return "" ; int len = s . length ( ) ; //提前计算字符串长度,降低时间复杂度 int flag ; //标记位,检验是否为回文用 vector < string > ss ; //存储满足条件的回文串,便于排序 for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i ; j < len ; j ++ ) { flag = 0 ; for ( int m = i , n = j ; m < n ; m ++ , n -- ) //检验子串是否是回文的for循环 { if ( s [ m ] != s [ n ] ) { flag = 1 ; } } if ( flag == 0 ) ss . push_back (

字符串常用技巧

半世苍凉 提交于 2020-01-28 02:12:46
一. toCharArray() 作用 将字符串转为字符数组 示例 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String sc = in.next(); char[] ch = sc.toCharArray(); for (int i = 0; i < ch.length; i++) { System.out.println(ch[i]); } } } 结果: abc a b c 二. substring() 作用 截取某段字符串。 示列 public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); String str1 = in.nextLine().substring(0,3); //截取下标为0,1,2的字符串 System.out.println("substring(0,3)的输出结果:"+str1); String str2 = str1.substring(1); //截取下标为1及其后面的字符串 System.out.println("substring(1)的输出结果:"

刷题5. Longest Palindromic Substring

不羁岁月 提交于 2020-01-26 12:30:08
一、题目说明 Longest Palindromic Substring,求字符串中的最长的回文。 Difficuty是Medium 二、我的实现 经过前面4个题目,我对边界考虑越来越“完善”了。 总共提交了5次: 第1、2次:Wrong Answer 主要是 "cbbd" 错误了,重复的判断逻辑上出了点小问题 第3、4次: Time Limit Exceeded 我本地代码运行没问题的,但是提交后,报错。给了一个超长的用例:

国庆练习3

牧云@^-^@ 提交于 2020-01-26 04:00:05
Benches CF 1042A There are n n benches in the Berland Central park. It is known that a i ai people are currently sitting on the i i-th bench. Another m m people are coming to the park and each of them is going to have a seat on some bench out of n n available. Let k k be the maximum number of people sitting on one bench after additional m mpeople came to the park. Calculate the minimum possible k k and the maximum possible k k. Nobody leaves the taken seat during the whole process. Input The first line contains a single integer n ( 1 ≤ n ≤ 100 ) — the number of benches in the park. The second