codevs 2980 买帽子 题解报告

24 篇文章 0 订阅
6 篇文章 0 订阅

噫本来今天高高兴兴

本来今天不想写博客来着,
哎,看见栋栋大神做题了,
我就跟风过去瞅瞅。。。

题目描述 Description
小A想买一顶新帽子,商店里有n个帽子 (1<=n<=100),每顶帽子上有一个字符串,字符串的长度为len (1<=len<=500)。她认为每顶帽子上的字符串看起来越对称则代表这顶帽子更漂亮。根据每个字符串,我们可以算出其对称系数k (即最长对称子序列的长度) 来比较各顶帽子在小A心中的漂亮程度。

  例如,字符串 character (k=5) 比 pollution (k=4) 更对称,apple (k=2) 比 pear (k=1) 更对称。

  现在给定n个字符串,请将它们按对称系数排序后从大小输出 (k相同时按字典序排序)。

输入描述 Input Description
输入数据第一行只有一个n,表示有个字符串。

接下来有n行,每行一个字符串。

输出描述 Output Description
输出有n行,每行一个字符串,表示按对称系数从大到小排序后的字符串,对称系数相同时按字典序排序。

样例输入 Sample Input
5

pineapple

banana

peach

coconut

character

样例输出 Sample Output
banana

character

pineapple

coconut

peach

数据范围及提示 Data Size & Hint
数据范围:

1<=n<=100

1<=len<=500

1<=k<=len

提示:

对称系数k是指最长对称子序列的长度,非最长对称子串的长度。

题意理解没啥难度
就是求一个字符串的最长回文子序列
注意是子序列不是字串、
字串的话完全可以暴力。
子序列就要用到DP了;;。

DP去死!

。。。。。

对于每个字符串,我们可以将他翻转;
生成另一个字符串;
之后,只需要求这两个字符串的最长公共子序列就好了,
用LCS求;

具体做法就是一个二维的DP矩阵
之后枚举每一个点。

如果对于这个i j 两字符串相同
f[i][j]=f[i-1][j-1]+1
else
f[i][j]=max(f[i-1][j],f[i][j-1]);

最后得到的f[len][len] 即为答案
注意从1开始枚举,防止数组越界。


呜啦啦啦啦

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstdlib>
#include<string>
#include<bitset>
#include<iomanip>
#include<deque>
#define INF 1000000000
#define fi first
#define se second
#define N 100005
#define P 1000000007
#define debug(x) cerr<<#x<<"="<<x<<endl
#define MP(x,y) make_pair(x,y)
using namespace std;
int n,m;
struct zqm
{
    string s;
    int k;
}q[101];
inline int get_num()
{
int num = 0;
char c;
bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
if (c == '-') flag = true;
else num = c - '0';
while (isdigit(c = getchar()))
num = num * 10 + c - '0';
return (flag ? -1 : 1) * num;
}
int js(string s)
{
    int len=s.size(),f[511][511];
    memset(f,0,sizeof(f));
    string ss;
    for(int i=0;i<len;i++)
    {
        ss[len-i-1]=s[i];
    }
    for(int i=1;i<=len;i++)
    {
        for(int j=1;j<=len;j++)
        {
            if(s[i-1]==ss[j-1])
            {
                f[i][j]=f[i-1][j-1]+1;
            }else
            {
                f[i][j]=max(f[i-1][j],f[i][j-1]);
            }
        }
    }
    return f[len][len];
}
bool cmp(zqm a,zqm b)
{
    if(a.k==b.k)
    {
        return a.s<b.s;
    }
    return a.k>b.k;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>q[i].s;
        q[i].k=js(q[i].s);
    }
    sort(q+1,q+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        cout<<q[i].s<<endl;
    }
}

。0.0.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值