HDU

本文介绍了一种算法,该算法将字符串转换为整数序列,并通过动态规划找到最长严格增子序列(LIS)。文章包含了一个示例程序,展示了如何通过注入映射将字符集转换为自然数集,并使用二分查找优化LIS的计算。

Lweb and String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
Lweb has a string S.

Oneday, he decided to transform this string to a new sequence. 

You need help him determine this transformation to get a sequence which has the longest LIS(Strictly Increasing). 

You need transform every letter in this string to a new number.

A is the set of letters of SB is the set of natural numbers. 

Every injection f:AB can be treat as an legal transformation. 

For example, a String “aabc”, A={a,b,c}, and you can transform it to “1 1 2 3”, and the LIS of the new sequence is 3. 

Now help Lweb, find the longest LIS which you can obtain from S.

LIS: Longest Increasing Subsequence. (https://en.wikipedia.org/wiki/Longest_increasing_subsequence)
 

Input
The first line of the input contains the only integer T,(1T20).

Then T lines follow, the i-th line contains a string S only containing the lowercase letters, the length of S will not exceed 105.
 

Output
For each test case, output a single line "Case #x: y", where x is the case number, starting from 1. And y is the answer.
 

Sample Input
2 aabcc acdeaa
 

Sample Output
Case #1: 3 Case #2: 4
 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 110000;
char str[N];
int a[N], visit[N];
vector<int>G;
int binary(int x);


int main()
{
    int ncase=1;
    int t;
    scanf("%d", &t);
    getchar();
    while(t--)
    {
        memset(str,'\0',sizeof(str));
        memset(visit,0,sizeof(visit));
        gets(str);
        int num=1;
        for(int i=0;str[i];i++)
        {
            int x=(str[i]-'a');
            if(visit[x]==0)
            {
                a[i]=num;
                visit[x]=num;
                num++;
            }
            else
            {
                a[i]=visit[x];
            }
        }
        int len=strlen(str);
        G.clear();
        for(int i=0;i<len;i++)
        {
            int tmp=binary(a[i]);
            if(tmp>=(int)G.size())
            {
                G.push_back(a[i]);
            }
            else
            {
                G[tmp]=a[i];
            }
        }
        int ans=(int) G.size();
        printf("Case #%d: %d\n",ncase++,ans);
    }
    return 0;
}




int binary(int x)
{
    int l=0, r=G.size()-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(G[mid]<x)
        {
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    return l;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值