POJ4044 Score Sequence 快排修改模板

http://poj.org/problem?id=4044

http://poj.org/ProblemDescriptions/jinghua.pdf题面

题意:对两组数据排序后寻找最长连续公共子序列,排序前应该先去重。再将最长连续公共子序列按个位数升序排序,个位数相同则小数在前。

解法:用set排序去重。再暴力找最长连续公共子序列即可。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const double eps=1e-8;
const double INF=1e50;
//const double pi=acos(-1);

#define N 1000005

int a[50],b[50],c[50];

bool cmp(int p,int q)
{
    int s,t;
    s=p%10;
    t=q%10;
    if (s!=t) return s<t;
    else return p<q;
}

int main()
{
    //freopen("a","r",stdin);

    int i,j,n1,n2,T,kk,k,s;
    set<int>a1;

    scanf("%d",&T);
    for (kk=1;kk<=T;kk++)
    {
        scanf("%d%d",&n1,&n2);

        a1.clear();
        for (i=1;i<=n1;i++)
        {
            scanf("%d",&j);
            a1.insert(j);
        }
        n1=0;
        set<int>::reverse_iterator it;
        for (it=a1.rbegin();it!=a1.rend();it++)
        {
            n1++;
            a[n1]=*it;
        }

        a1.clear();
        for (i=1;i<=n2;i++)
        {
            scanf("%d",&j);
            a1.insert(j);
        }
        n2=0;
        for (it=a1.rbegin();it!=a1.rend();it++)
        {
            n2++;
            b[n2]=*it;
        }
        a[n1+1]=-1;
        b[n2+1]=-2;

        i=j=1;
        int max=0;
        while (i<=n1 && j<=n2)
        {
            if (a[i]==b[j])
            {
                k=1;
                while (a[i+k]==b[j+k] && (i+k)<=n1 && (j+k)<=n2) k++;
                if (a[i+k]!=b[j+k]) k--;
                if (k>=max)//注意这里不是k>max!!
                {
                    max=k+1;
                    for (s=1;s<=max;s++) c[s]=a[i+s-1];
                }

                i++;
                j++;
            }
            else if (a[i]>b[j]) i++;
            else j++;
        }

        if (max==0)
        {
            printf("NONE\n");
            continue;
        }
        printf("%d",c[1]);
        for (i=2;i<=max;i++) printf(" %d",c[i]);

        printf("\n");
        sort(c+1,c+max+1,cmp);
        printf("%d",c[1]);
        for (i=2;i<=max;i++) printf(" %d",c[i]);
        printf("\n");
    }


    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值