首页 > 试题广场 >

给定一个query和一个text,均由小写字母组成。要求在t

[问答题]
给定一个query和一个text,均由小写字母组成。要求在text中找出以同样的顺序连续出现在query中的最长连续字母序列的长度。例如,query为 "acbac",text为"acaccbabb",那么text中的"cba"为最长的连续出现在query中的字母序列,因此,返回结果应该为其长度3。请注意程序效率。
推荐
求两个字符串的最大公共字串问题

贴代码结构乱了,截图吧




编辑于 2015-07-02 15:10:17 回复(4)
发表于 2015-08-19 20:52:40 回复(1)
发表于 2015-08-21 20:19:14 回复(1)
#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;
} 

发表于 2018-06-05 17:04:36 回复(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;

}

发表于 2017-08-24 11:12:59 回复(0)

import java.util.ArrayList;

public class GetLongest_yhy {

public static void main(String[] args) {
myGetLong("acbac", "acaccbabb");
}

public static void myGetLong(String query, String text) {
int i, j, k;
int qlen = query.length();
ArrayList<String> list = new ArrayList<String>();
for (i = qlen; i > 0; i--) {
for (j = 0; j < qlen; j++) {
if(i+j>qlen) continue;
list.add(query.substring(j, j+i));
}
}
System.out.println(list);
for (k = 0; k < list.size(); k++) {
if (text.contains(list.get(k))) {
System.out.println(text + "中的" + list.get(k) + "为最长的连续出现在" + query
+ "中的字母序列");
break;
}
}
}
}

发表于 2015-08-22 22:50:19 回复(0)
<pre class="prettyprint lang-cpp">#include &lt;stdio.h&gt; #include &lt;string.h&gt; int main() { char query[256],text[256]; int i,j,len1,k=0,len2,b[256]={0}; scanf("%s",query); scanf("%s",text); len1=strlen(query); len2=strlen(text); for(i=0;i&lt;len1;i++) { for(j=0;j&lt;len2;j++) { if(text[j]==query[j]) b[k]+=1; else continue; } k++; } int max=0; for(i=0;i&lt;k;i++) if(b[i]&gt;max) max=b[i]; printf("%d\n",max); return 0; } </pre> <br />
发表于 2015-08-21 17:45:23 回复(0)
左大神有说过这题,如果没理解错的话,时间复杂度O(n2),空间复杂度O(1),动态规划的二维数组可以省去
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;
}

发表于 2015-08-21 00:47:04 回复(0)
<pre class="prettyprint lang-java"> </pre> <div> import java.util.Scanner; </div> <div> public class Exam { </div> <div> &nbsp; &nbsp; public static void main(String[] args){ </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; Scanner scan = new Scanner(System.in); </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; String query = scan.nextLine(); </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; String text = scan.nextLine(); </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; int initLength = query.length() &gt; text.length() ? query.length() : text.length(); </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; for(;initLength&gt;0;initLength--){ </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int start=0,end=initLength;end&lt;=query.length()&amp;&amp;initLength&gt;0;start++,end++){ </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String stringOfQuery = query.substring(start,end); </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int st=0,ed=initLength;ed&lt;=text.length()&amp;&amp;initLength&gt;0;st++,ed++){ </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String stringOfText = text.substring(st,ed); </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(stringOfQuery.equals(stringOfText)){ </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(initLength); </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initLength = 0; </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } </div> <div> &nbsp; &nbsp; &nbsp; &nbsp; } </div> <div> &nbsp; &nbsp; } </div> <div> } </div>
发表于 2015-08-19 09:33:06 回复(0)
<pre class="prettyprint lang-java">public class Test { static int askMin(String query, String text, int Size){ boolean isHava = false; for(int i = 0; i &lt; text.length()-Size+1; i++){ String a = text.substring(i, Size+i); for(int j = 0; j &lt; query.length() - Size + 1; j++){ String qry = query.substring(j, Size + j); if(a.equals(qry)){ isHava = true; break; } } } if (isHava) { return Size; }else { return askMin(query, text, Size-1); } } public static void main(String args[]){ System.out.println(askMin("acbac", "acaccbabb", "acbac".length())); } }</pre> <div> <br /> </div> <div> <br /> </div> <div> <br /> </div>
发表于 2015-08-18 11:17:57 回复(0)
<pre class="prettyprint lang-java">&lt;pre class="prettyprint lang-java"&gt;public class FindIndexString { public static int index_KMP(String str, String subStr, int pos){ char S[] = str.toCharArray(); char T[] = subStr.toCharArray(); int i = pos; int j = 0; int next[] = new int[T.length]; get_next(T, next); while(i&amp;lt;S.length&amp;amp;&amp;amp;j&amp;lt;T.length){ if(j==-1||S[i]==T[j]){ i++; j++; }else{ j = next[j]; } } if(j==T.length) return i-j; else return -1; } public static void get_next(char T[], int next[]){ int i=0; next[i] = -1; int j=-1; while(i&amp;lt;(T.length-1)){ if(j==-1||T[i]==T[j]){ i++; j++; if(T[i]!=T[j]){ next[i] = j; } else next[i] = next[j]; } else j = next[j]; } } public static int getMostString(String Query, String Text){ char text[] = Text.toCharArray(); int n = Query.length(); int m = text.length; int i = 0; int j = Math.min(m, n); while(j&amp;gt;0){ while(i+j&amp;lt;=m){ String str = Text.substring(i, i+j); if(index_KMP(Query, str, 0)!=-1) return j; else i++; } i = 0; j--; } return -1; } } &lt;/pre&gt; &lt;br /&gt;</pre> <br />
发表于 2015-08-15 22:17:04 回复(0)
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 + "中的字母序列");
	}
}


发表于 2015-08-15 16:01:19 回复(0)
思路:
先把s1的所有子串找出来放在set里面,然后在s2中找出所有能和set集合匹配的子串;
然后同时用Map来将匹配的子串的长度和子串映射起来,用sort函数找出匹配子串的最大长度,在根据map就可以找出来
#include <iostream>

#include <vector>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;

void get_substr_set(char *str1, char *str2)
{
       string  s1(str1);
       string  s2(str2);
       set<string> v1;
       vector<int> a;
       set<string>::iterator it;
       map<int,string> maa;
       int m = 0;
       for(int j =0;j <s1.size();j++)
       {
          if(j == 0) m = s1.size() -1;
          else m = s1.size() -j;
          for(int i =1;i <=m;i++)
          {
             v1.insert(s1.substr(j,i));
          }
       }

       for (it = v1.begin(); it!=v1.end(); ++it)
       {
           if(s2.find(*it)!=string::npos)   { a.push_back((*it).length());  maa[(*it).length()] = *it; cout<<"*it="<<*it<<"   ";}
       }
       sort(a.begin(),a.end());
       cout<<"find^^="<<  maa[a[a.size()-1]]<<"\n";

}
int main ()
{
  char str1[] =  "acbac";
  char str2[] = "acaccbabb";
  get_substr_set(str1, str2);


  return 0;
}

发表于 2015-07-14 23:03:28 回复(0)
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;
        
    }


发表于 2015-05-10 22:57:23 回复(4)
xxj头像 xxj
公共子窜问题
使用矩阵对角线原理实现
构造矩阵sizeof(query)长,sizeof(text)宽
依次遍历,对应矩阵下标xy中看query[x]是否等于矩阵下标text[y]
如果相等,则矩阵xy位置的值为矩阵x-1,y-1下标值(超出为0)加1

最后构造完成后找出其中矩阵中最大的值,xy
然后一次下标对角线回溯到0(可递归或倒序输出)

完成
发表于 2015-02-25 16:09:39 回复(0)
#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;
}
编辑于 2015-01-31 12:13:01 回复(1)