问个问题:
如何按照顺序提取出两个字符串中的相同部分?
比如:
字符串a:asd34ghf7asdJHYio-)00><kds
字符串b:bsd25ghf9sasshio98s<kds
要求得到结果:sd ghf as io <kds
注意:是按顺序得到相同字符串,比如:as 在字符串a中有2处,但是,只能取的第二处的

解决方案 »

  1.   

    先求b的子串,将b的子串再与a行进匹配。这是用以前学的数据结构的思想。
      

  2.   


    string a = "abcd";
    string b = "bbcd";
    string sameStr = string.Empty;
    int max = a.Length>b.Length?a.Length:b.Length;
    for(int i = 0;  i < max ; i++)
    {
       if(a[i] == b[i] )
        sameStr.Append(a[i]);
    }
      

  3.   

    csShooter(Sharp Shooter) 
    这样做是不符合要求的,按照你的程序:
    字符串a:asd34ghf7asdJHYio-)00><kds
    字符串b:bsd25ghf9sasshio98s<kds得到结果是: sd ghf 
    不符合我的要求
      

  4.   

    string a="asd34ghf7asdJHYio-)00><kds";
    string b="bsd25ghf9sasshio98s<kds";
    ArrayList al=new ArrayList();
    for(int i=0;i<a.Length;i++)
    {
    int k=i;
    for(int j=0;j<b.Length;j++)
    {
    string temp="";
    while(k<a.Length&&j<b.Length)
    {
    try
    {
    if(a[k].Equals(b[j]))
    {
    temp+=a[k];
    k++;
    j++;
    }
    else
    break;
    }
    catch{break;}
    }
    if(temp.Length>1&&!al.Contains(temp))
    {
    if(al.Count==0)
    al.Add(temp);
    else
    if(!(al[al.Count-1].ToString().IndexOf(temp)>-1))
    al.Add(temp);
    }
    }
    }
    //al
      

  5.   

    cancerser(都是混饭吃,记得要结帖) :
    感谢你的程序,但是,按照你的代码,还是得出的不是我要求的结果,你得出的结果是:
    as d这个问题是复杂的哦~
      

  6.   

    楼主的题目不太清楚..字符串a:asd34ghf7assdJHYio-)00><kds(第二个as后多加了一s)
    字符串b:bsd25ghf9sasshio98s<kds
    这样子的话你又想得到什么呢?和匹配模式有关..向左匹配还是向右匹配...
      

  7.   

    字符串a:asd34ghf7assdJHYio-)00><kds(第二个as后多加了一s)
    字符串b:bsd25ghf9sasshio98s<kds
    这样子的话你又想得到什么呢?按照我的要求,结果是:
    sd ghf ass io <kds 就是要得到最大化 公用字符串序列
      

  8.   

    两个字符串 与或求值
    并根据结果(真值位应该都是相同部分的index),直接获取该位的char 就可以了
      

  9.   

    AFatPig(大肥猪)
    你没看清楚我的问题
      

  10.   

    呵呵,运行结果绝对正确。sdghfasio<kds
    Press any key to continue使用字符串b匹配a,函数maxmatch找出当前字符串para长度内p2匹配p1匹配最多的p2的位置。
    纯C语言,没写注释,凑合着看哈。#include <stdio.h>
    #include <string.h>
    void main()
    {
        char a[]="asd34ghf7asdJHYio-)00><kds";
        char b[]="bsd25ghf9sasshio98s<kds";
        char c[255]={0};
        char *p1=a,*p2=b;
        int count=0;
        int pos=0;
        unsigned int para=5;
        
        while(*p2)
        {
            if(strchr(p1,*p2)!=NULL)
            {
                if(strlen(p1)<para) para=strlen(p1);
                if(strlen(p2)<para) para=strlen(p2);
                if((pos=maxmatch(p1,p2,para))!=0)
                {
                    p2=p2+pos;
                    continue;
                }
                c[count++]=*p2;
                p1= strchr(p1,*p2)+1;
            }
            p2++;
        }
        printf("%s",c);
    }/*找出para长度内最匹配的字符串的p2的位置*/
    int maxmatch(char* p1,char* p2,int para)
    {
        int i;
        int matched=0;
        int maxmatched=0;
        
        int pos=0;
        int trypos=0;
        char *tmp1=p1;
        char *tmp2=p2;
        printf("%s  ",p1);
        printf("%s\n",p2);
        for(trypos=0;trypos<para;trypos++)
        {
            tmp2=&p2[trypos];
            tmp1=p1;
            matched=0;
            i=0;
            while(i<para-trypos)
            {
                if(strchr(tmp1,*tmp2)!=NULL)
                {
                    matched++;
                    tmp1= strchr(tmp1,*tmp2)+1;
                }
                tmp2++;
                i++;
            }
            if(matched>maxmatched)
            {
                maxmatched=matched;
                pos=trypos;
            }
        }
        return pos;
    }
      

  11.   

    ShawnSun(babycat)
    你的程序对于这个我给出的字符串是正确的,但是,其他字符串就不正确了,我如果最长字串长超过5个,你的程序就给出了错误的答案。
      

  12.   

    那这个呢  
    a:abcdcba
    b:abcdxxdcba
    是abcd ba (从左向右.但较短)
    还是 abc dcba(从右向左.但较长)
      

  13.   

    是abcd cba (从左向右)
    还是 abc dcba(从右向左)
      

  14.   

    a:abcdcba
    b:abcdxxdcba结果是: abcd cba仅从左向右匹配本提的问题从新写一下:
     求两个字符串的最长公共字符串(Longest Common Chars)序列(左向右匹配)
      

  15.   

    字符串a:asd34ghf7asdJHYio-)00><kds
    字符串b:bsd25ghf9sasshio98s<kdsf7asdJHYio
    请楼主说明要求的结果是什么?
      

  16.   

    to :
    问个问题:
    如何按照顺序提取出两个字符串中的相同部分?
    比如:
    字符串a:asd34ghf7a sd JHYio-)00><kds
    字符串b:bsd25ghf9s as shio98s<kds
    要求得到结果:sd ghf as io <kds
    注意:是按顺序得到相同字符串,比如:as 在字符串a中有2处,但是,只能取的第二处的按顺序 ,怎么会取到 as ?本提的问题从新写一下:
     求两个字符串的最长公共字符串(Longest Common Chars)序列(左向右匹配)
    是一样的问题吗?
      

  17.   

    AFatPig(大肥猪):
    不是按照字符串相同的位置来提取的,
    比如:
    字符串a:asd34ghf7asd JHYio-)00><kds
    字符串b:bsd25ghf9sas shio98s<kds提取的到的可以将a、b字符串写的形象一点:字符串a:a sd 34 ghf 7  as dJHY io -)00> <kds
    字符串b:b sd 25 ghf 9s as sh   io   98s <kds所以实际问题就是: 求两个字符串的最长公共字符串(Longest Common Chars)序列(左向右匹配)
      

  18.   

    这下总可以了吧....
    有不正确的反例的话,可以贴出来,然后再修正。
    #include <stdio.h>
    #include <string.h>char* getMaxMatched(char* a,char* b,int para_in);
    int maxmatch(char* p1,char* p2,int para);void main()
    {
        char a[]=/*"bsd25ghf9sasshio98s<kds";//*/"abctuvwxyzdefghithisisxdmiscne";
        char b[]=/*"asd34ghf7asdJHYio-)00><kds";//*/"xdabcdefghituvwxyzthisxismefine";
        
        printf("%s\n",getMaxMatched(a,b,1));
        //maxmatch("efin","ifcn",4);
    }
    char* getMaxMatched(char* p1,char* p2,int para_in)
    {
        static char c[255]={0};
        int count=0;
        int pos=0;
        int para=para_in;
        int ncount=0;
        
        while(*p2&&*p1)
        {
            if(*p2==*p1)
            {
                c[count++]=*p2;
                printf("%c ",*p2);
                p2++;
                p1++;
                para=para_in;
                ncount=0;
            }
            else
            {
                pos=maxmatch(p1,p2,para);
                if(pos==0)
                {
                    p1=strchr(p1,*p2);
                }
                else if(pos==-1)
                {
                    para=(para+(++ncount))*2;
                    //printf("pos=%d",pos);
                    //p2++;
                    //p1++;
                }
                else
                {
                    p2=p2+pos;
                    p1=strchr(p1,*p2);
                    continue;
                }
            }
        }
        return c;
    }/*&Otilde;&Ograve;&sup3;&ouml;para&sup3;¤&para;&Egrave;&Auml;&Uacute;×&icirc;&AElig;&yen;&Aring;&auml;&micro;&Auml;×&Ouml;·&ucirc;&acute;&reg;&micro;&Auml;p2&micro;&Auml;&Icirc;&raquo;&Ouml;&Atilde;*/
    int maxmatch(char* p1,char* p2,int para)
    {
        int i;
        int j;
        int k;
        
        int matched=0;
        int maxmatched=0;
        
        int pos=0;
        int trypos=0;    //printf("para=%d ",para);
        //printf("%s    %s    \n",p1,p2);
        for(trypos=0;trypos<para&&trypos<strlen(p2);trypos++)
        {
            matched=0;
            i=trypos;
            j=0;
            k=0;        while(i<para&&i<strlen(p2))
            {
                while(j<para&&j<strlen(p1))
                {
                    if(p2[i]==p1[j])
                    {
                        matched++;
                        j++;
                        k=j;
                        break;
                    }
                    else
                    {
                        j++;
                    }
                }
                i++;
                j=k;
            }
            if(matched>=maxmatched)
            {
                maxmatched=matched;
                pos=trypos;
            }
        }
        //printf("pos=%d,maxmatched=%d\n",pos,maxmatched);
        if(maxmatched==0)
        {
            return (-1);
        }
        else
        {
            return pos;
        }
    }
      

  19.   

    楼主请搜索一下LCS算法,这是最大公共子字符串问题。根据算法自己很快都可以写出这段程序来。我这里有一份.net代码,不过经过了一点点改动,所以不适合你。:)
      

  20.   

    我已经解决了。c#程序,见我的blog,谢谢大家热心帮助。