#include<iostream> #include<string> #include<vector> using namespace std; int main() { string text = "acaccbabb"; string query = "acbac"; int tlen = text.length(); int qlen = query.length(); int* dp = new int[qlen]; int maxlen = -1; int maxInd = -1; memset(dp, 0, sizeof(int)*qlen); for (int i = 0; i < tlen;++i) for (int j = qlen - 1; j >= 0; --j) { if (text[i] == query[j]) { if (i == 0 || j == 0) dp[j] = 1; else { dp[j] = dp[j - 1]+1; } } else { dp[j] = 0; } if (dp[j] > maxlen) { maxlen = dp[j]; maxInd = j; } } for (i = maxInd - maxlen + 1; i <= maxInd; ++i) cout << query[i]; cout << endl; return 0; }
#include<iostream> #include<string> using namespace std; int main() { string query, text; cin >> query >>text; int m = query.size(); int n = text.size(); int max = 0; if (m < 1 || n < 1) return 0; int** array = new int*[m]; for (int i = 0; i < m; ++i) { array[i] = new int[n](); } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (query[i] == text[j]) { if (i == 0 || j == 0) array[i][j] = 1; else { array[i][j] = array[i - 1][j - 1] + 1; } if (max < array[i][j]) max = array[i][j]; } } } cout << max << endl; return 0; }
char* findMaxMatch(const char* text,int len1,const char* query,int len2){ //异常处理 int row = 0,col = len2 -1; int len = 0,ends = 0,maxlen = 0; while(row < len2){ int i = row,j=col; len = 0; while(i<len2 && j<len1){ if(text[j] != query[i]) len=0; else len++; if(len>maxlen){ maxlen = len; ends = j; } i++;j++; } if(col >= 0) col--; else if(row < len2) row++; } char* maxMatch = new char[maxlen+1]; for(int i = 0;i<maxlen;i++){ maxMatch[i] = text[ends-maxlen+1+i]; } maxMatch[maxlen] = '\0'; return maxMatch; }
package com.java.test; import java.util.ArrayList; public class GetLongest { public static void main(String[] args) { myGetLong("acbac", "acaccbabb"); } public static void myGetLong(String query, String text) { int i, j, k; int num = 0; int m = 0; int qlen = query.length(); ArrayList<String> list = new ArrayList<String>(); for (i = 0; i < qlen; i++) { for (j = 1; j <= qlen; j++) { if (j <= i) { j = i + 1; } list.add(query.substring(i, j)); } } for (k = 0; k < list.size(); k++) { if (text.contains(list.get(k)) && list.get(k).length() > num) { num = list.get(k).length(); m = k; } } System.out.println(text + "中的" + list.get(m) + "为最长的连续出现在" + query + "中的字母序列"); } }
思路:先把s1的所有子串找出来放在set里面,然后在s2中找出所有能和set集合匹配的子串;
#include <iostream>
public int getLong(String query,String text){ if(text.length()==0||query.length()==0){return 0;} int start=0; int end=1; int longest=0; int tempSum=0; String temp=""; while(end<=text.length()){ temp=text.substring(start,end); if(!query.contains(temp)){ tempSum=0; start=(end-1==start)?end:end-1; end=start+1; } else{ tempSum++; if(tempSum>longest){longest=tempSum; System.out.println("------"+temp+longest); //可以用这句话来跟踪最大长度的子字符串 } end++; } } return longest; }
#include <iostream> #include <string> #include <vector> using namespace std; int main(){ string query, text; cin >> query >> text; vector<int> flag; for (int i = 0; i < query.size(); ++i){ if (text.find(query[i]) != string::npos) flag.push_back(i); } if (flag.size() == 1){ printf("1\n"); return 0; } int ans = 0, len = 0; for (int i = 0; i < flag.size(); i++){ string str = ""; str += query[flag[i]]; for (int j = i + 1; j < flag.size(); j++){ if (flag[j] - flag[j - 1] == 1){ str += query[flag[j]]; if (str.size() <= len) continue; if (text.find(str) != string::npos){ len = str.size(); if (len > ans) ans = len; } else break; } else break; } } printf("%d\n", ans); return 0; }