5. 查找两个字符串中含有的最长字符数的公共子串. package chapter5; import java.util.Scanner; public class demo5 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String a=sc.next(); String b=sc.next(); int max=0; int maxi=0; int arr[][]=new int[…
要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的) public class Main03{ // 求解两个字符号的最长公共子串 public static String maxSubstring(String strOne, String strTwo){ // 参数检查 if(strOne==null || strTwo == null){ return null; } if(strOne.equals("&qu…
问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. 例如:“acbbsdef”和"abbsced"的最长公共字符串是“bbs” 算法思路: 1.把两个字符串分别以行和列组成一个二维矩阵. 2.比较二维矩阵中行和列对应的每个点的字符是否相同,是设置这个点为1,否设置这个点为0. 3.通过查找值为1的最长对角线来找到最长公共字符串. 通过上面str1和str2两个字符串,分别得出以行和列组成的一个二维矩阵如下图: 从上图可以看到,str1和str2共有3个公共子串&qu…
Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 31904   Accepted: 12876 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days…
Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speech (this story is fully described in the problem "Freedom of speech"), another freedom - the freedom of choice - came down on them. In the near fu…
由于python中的for循环不像C++这么灵活,因此该用枚举法实现该算法: C="abcdefhe" D="cdefghe" m=0 n=len(C) E=[] b=0 while(m<n): i=n-m while(i>=0): E.append(C[m:m+i]) i-=1 m+=1 for x in E: a=0 if x in D: a=len(x) c=E.index(x) if a > b:#保存符合要求的最长字符串长度和地址 b=a…
把s1,s2拼接,求Height.相邻的Height判断左右串起点是否在两个串中,另外对Height和s1.length()-SA[i-1]取min. #include <iostream> #include <cstring> #include <string> #include <queue> #include <vector> #include <map> #include <set> #include <st…
这个是华为OJ上的一道题目.首先,如果我们用java写代码,华为OJ有以下三条规则需遵守,否则编译无法通过或者用例无法通过,规则如下: (1)一定不可以有包名: (2)主类名只能为Main: (3)不可以输出与结果无关的信息. 好了,按照以上规则,我们写出来的代码如下(此代码不是最优的,只是用来记录华为OJ上java代码的书写规则): import java.util.Scanner; public class Main { public static void main(String[] ar…
1.注意每两个串之间的连接符要不一样. 2.分组的时候要注意最后一组啊!又漏了! 3.开数组要考虑连接符的数量.100010是不够的至少要101000. #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> using namespace std; ; int n,cl,sl,ans,tt,c[N],tl[N],tr[N],al[N],ar[N],rk[N],Rs[N]…
http://tianyunpu2008.blog.163.com/blog/static/6559379920089162236915/…
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the set of lowercase letters. Substring, also called factor, is a consecutive sequence of characters occur…
package tek; Java算法——求出两个字符串的最长公共字符串 /** * @Title: 问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. * @author 匹夫(在YW的数据基础上再次延伸) * @date 2017年4月19日 下午8:08:37 */ public class Demo1 { /* //减0 个字符 内循环1次 k2.substring(0, 7);//外循环第1次_内循环第1次 //减一个字符 内循环2次 k2.substring(0,…
题目链接:https://vjudge.net/problem/POJ-2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 33144   Accepted: 13344 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A…
public class String_intern { public static void main(String[] args) { String old="aaaaabc1"; String ne="aabc1"; String oak; for (int i = 0; i <ne.length() ; i++) { //z 只能取到 ne.length() 通过subString可以取到最后 ne.length() -1 for (int j = 0…
/* * 3,两个字符串中最大相同的子串. * "qwerabcdtyuiop" * "xcabcdvbn" *  * 思路: * 1,既然取得是最大子串,先看短的那个字符串是否在长的那个字符串中. *   如果存在,短的那个字符串就是最大子串. * 2,如果不是呢,那么就将短的那个子串进行长度递减的方式取子串,去长串中判断是否存在. *   如果存在就已找到,就不用在找了. * 3.先找最大的子串,再递减子串找,找到,就停止 */ 原理图如图:…
一.问题描述 给定两个字符串,求解这两个字符串的最长公共子序列(Longest Common Sequence).比如字符串1:BDCABA:字符串2:ABCBDAB.则这两个字符串的最长公共子序列长度为4,最长公共子序列是:BCBA 二.算法求解 这是一个动态规划的题目.对于可用动态规划求解的问题,一般有两个特征:①最优子结构:②重叠子问题 ①最优子结构 设X=(x1,x2,...,xn)和Y=(y1,y2,...,ym)是两个序列,将X和Y的最长公共子序列记为LCS(X,Y) 找出LCS(X…
字符串中连续出现最多的子串 & 字符串中最长反复子串 字符串中连续出现最多的子串 & 字符串中最长反复子串,这两个问题都能够用后缀数组来表示,至于后缀数组能够參考编程珠玑P156:后缀数组就是定义一个数组指针,分别指向字符串中的相应位置,例如以下: a b c a b c a b c d e .substr[0] b c a b c a b c d e ....substr[1] c a b c a b c d e .......substr[2] a b c a b c d e ....…
http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: 分析: 给定两个字符串 S 和 T ,求出最长公共子串,公共子串定义为在 S 和 T 中 都作为子串出现过的字符串 X . 我们为字符串 S 构造后缀自动机. 我们现在处理字符串 T ,对于每一个前缀都在 S 中寻找这个前缀的最长后缀.换句话 说,对于每个字符串 T 中的位置,我们想要找到这个位置…
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. 随意做.这里面只写一下我对后缀自动机做法的理解. 首先,我们假设两个串分别为A串和B串,我们先对建立出A串的后缀自动机,然后对于B串的每一位,我们进行如下的操作:首先从第1位开始,Parent树上的位置在root,那么对于每一次操作,如果当前结点的字符可以匹配当前B串中所考虑到的字符,那么自然就l…
                                                                          Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10800   Accepted: 2967 Description You may have wondered why most extraterrestrial life forms resemble h…
例子 "abmadsefadd"  最长长度为7 "avoaid"           最长长度为3 思路 空间换时间hashTable,起始位置设为beg.初始化全局最大值0.开辟字符数组,起初标为0. 访问数组时 如果该字符在hashTable对应的哈希值为1,则计算当前位置到beg的距离,并且把beg赋值为beg+1.如果大于全局最大值,则替换全局最大值 如果该字符在hashTable对应的哈希值为0,则置1 参考代码 #include <iostrea…
Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 26601   Accepted: 10816 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days…
Blue Jeans HDOJ-3080 本题使用的是KMP算法加暴力解决 首先枚举第一个字符串的所有子串,复杂度为O(60*60),随后再将每个子串和所有剩下的m-1个字符串比较,看是否存在这个子串 若所有的字符串都有该子串,则看是否是最长的,如果是等长的再看是否是字典序最小的. 本题我wa了很多发的地方就是子串的len即长度那里,没有加等号,所以...无奈 还需要注意的是使用快速输入输出,才不会超时. #include<iostream> #include<cstdio> #i…
还是直接上转移方程: 动规只能解决O(n^2)的最长公共子串问题 使用后缀数组或者SAM可以高效地解决这个问题 所以,对于这个问题,动规的代码就不给出了 直接给出SAM的实现,也为以后学习SAM打下一个基础 具体做法是,对一个串建后缀自动机,把另一个串在自动机上跑,维护一下最大的匹配的长度就好了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,ans; ]…
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在学习后缀数组的时候已经做过一遍了,但是现在主攻字符串hash,再用字符串hash写一遍. 这题的思路是这样的: 1)取较短的串的长度作为high,然后二分答案(每次判断长度为mid=(low+high)>>1是否存在,如果存在就增加下界:不存在就缩小上界): 2)主要是对答案的判断(judge函数…
1这道题目就是给定两个字符串,然后求这两个字符串的最长公共子串的最大长度,假设我的f()方法是来求两个字符串的最大公共子串,从头开始逐一比较,如果相等,则 继续调用这个方法,使得递归的长度+1,如果不相等,则只要比较s1截掉一个和s2比较,和s2截掉和s1比较,两个中的最大者,如果s1或者s2中有一个长度为0,则最大公共长度就是0,return 2.代码示例: package zzl; public class 最长公共子串 { public static void main(String[]…
题目 获取两个字符串中最大相同子串 前提 两个字符串中只有一个最大相同子串 解决方案 public class StringDemo { public static void main(String[] args) { String str1 = "abcwerthelloyuiodefabcdef"; String str2 = "cvhellobnm"; String str3 = getMaxSameString(str1, str2); System.out…
题目描述 写一函数,将两个字符串中的元音字母复制到另一个字符串,然后输出. 输入 一行字符串 输出 顺序输出其中的元音字母(aeiuo) 样例输入 abcde 样例输出 ae 编码: #include<stdio.h> #include<stdlib.h> #include<string.h> char str1[100]; char str2[100]; char str[6] = {'a' , 'e' , 'i' , 'o' , 'u'}; int main() {…
连接两个字符串中的不同字符   给出两个字符串, 你需要修改第一个字符串,将所有与第二个字符串中相同的字符删除, 并且第二个字符串中不同的字符与第一个字符串的不同字符连接 思路:遍历两个字符串,找到互相不重复的字符 class Solution { public: /* * @param : the 1st string * @param : the 2nd string * @return: uncommon characters of given strings */ string conc…
题目链接:https://vjudge.net/problem/SPOJ-PHRASES PHRASES - Relevant Phrases of Annihilation no tags  You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your isl…