• 【C++】子序列匹配问题


    /*
    一个串的“子序列”(subsequence)是将这个串中的一些字符提取出来得到一个新串,并且不改变它们的相对位置关系。例如,串"XDoi","XianYu!","TaiQiangLa!","loa"都是串"XianYuDalaoTaiQiangLa!"的子序列。
    
    我们说串t是串s1和s2的公共子序列,当且仅当t是s1的子序列且t是s2的子序列。定义串s1和s2的相似度为它们最长公共子序列的长度。
    
    现在给定一个文本串S和一组模式串T[1]、T[2]、……、T[n]。求T[i]中和S具有最高相似度的那个,然后输出最高的相似度。S和所有的T[i]都只含有小写字母。
    
    输入规则:先是一行字符串S。第二行是n(1<=n<=100)。第三行以降的n行是n个模式串T[1]...T[n]。S和所有的T[i]的长度都不超过2000.
    
    Sample Input:
    abcdef
    
    4
    
    acfaff
    
    appont
    
    emmm
    
    bdxeuf
    
    Sample Output:
    bdxeuf
    
    4
    
    Description:
    串abcdef和bdxeuf的最长公共子序列是bdef,长度为4.
    */
    
    
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int M=2010;
    
    int result[100];
    
    /*
    void match(char *a,char *b,int s,int t,int i)
    {
        int x=s;
        int y=t;
        while(a[x]!='')
        {
            while(b[y]!='')
            {
                if(a[x]==b[y])
                {
                    result[i]++;
                    match(a,b,x+1,y+1,i);
                    cout<<b[y]<<endl;
                }
                y++;
            }
            x++;
        }
        
    }
    */
    
    void match(char *a,char *b,int s,int t,int i)
    {
        int x=0;
        for(s=0;a[s]!='';s++)
        {
            for(t=x;b[t]!='';t++)
            {
                if(a[s]==b[t])
                {
                    result[i]++;
                    x++;
                    //cout<<b[t]<<endl;
                    break;
                }
            }
        }
    }
    
    /*
    bool compare(int a,int b)//降序为> 
    {
        return a>b;
    }
    */
    int main()
    {
        char a[M];
        char b[100][M];
        memset(a,'',sizeof(a));
        int n;
        
        while(scanf("%s",&a))
        {
            memset(b,'',sizeof(b));
            memset(result,0,sizeof(result));
            cin>>n;
            for(int i=0;i<n;i++)
                scanf("%s",&b[i]);
            for(int i=0;i<n;i++)
            {
                match(a,b[i],0,0,i);
                //cout<<"result:"<<result[i]<<";"<<b[i]<<endl;
            }
            /*
            sort(result,result+n,compare);
            cout<<result[0]<<endl;
            memset(a,'',sizeof(a));
            */
            int max=result[0];
            int max_num=0;
            for(int i=1;i<n;i++)
            {
                if(result[i]>max)
                {
                    max_num=i;
                    max=result[i];
                }
            }
            cout<<b[max_num]<<endl<<max<<endl;
            memset(a,'',sizeof(a));
        }
        
        return 0;
    }

     代码运行说明:

    tz@HZAU

    2019/3/7

  • 相关阅读:
    elasticsearch官方文档摸索
    nginx报错upstream sent invalid chunked response while reading upstream
    LRU算法的实现
    linux命令小计
    【阅读笔记】深入java虚拟机-第三部分-虚拟机执行子系统
    spring-session-data-redis导致跨域session失效
    ReentrantLock源码解读
    AbstractQueuedSynchronizer(AQS源码解读)
    Object中wait()、notify()、notifyAll()
    redis(单机模式)分布式锁的实现【已废弃】
  • 原文地址:https://www.cnblogs.com/acm-icpcer/p/10492772.html
Copyright © 2020-2023  润新知