Codevs 2980 买帽子

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

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

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

Output

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

Sample Input

5
pineapple
banana
peach
coconut
character

Sample Output

banana
character
pineapple
coconut
peach

Source


题解

求一个字符串的最长回文子序列,求它和它逆置的最长公共子序列即可

然后字典序排序……

代码

       
       
        
        
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define pi acos(-1.0)
#define maxn (100 + 100)
#define inf 50000
#define mol 1000000007
#define Lowbit(x) (x & (-x))
using namespace std ;
typedef long long int LLI ;

struct node {
char str [ 550 ];
int va ;
} op [ maxn ];

char Copy [ 550 ];
int dp [ 550 ][ 550 ];

bool cmp ( node a , node b ) {
if ( a . va != b . va ) return a . va > b . va ;
int lena = strlen ( a . str );
int lenb = strlen ( b . str );
for ( int i = 0 ; i < min ( lena , lenb ); i ++ ) {
if ( a . str [ i ] != b . str [ i ]) return a . str [ i ] < b . str [ i ];
}
return lena < lenb ;
}

int main () {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n ;
scanf ( "%d" , & n );
for ( int i = 0 ; i < n ; i ++ ) {
scanf ( "%s" , op [ i ]. str );
strcpy ( Copy , op [ i ]. str );
int len = strlen ( Copy );
reverse ( Copy , Copy + len );
memset ( dp , 0 , sizeof ( dp ));
for ( int k = 0 ; k < len ; k ++ ) {
for ( int j = 0 ; j < len ; j ++ ) {
if ( Copy [ k ] == op [ i ]. str [ j ])
dp [ k + 1 ][ j + 1 ] = dp [ k ][ j ] + 1 ;
else
dp [ k + 1 ][ j + 1 ] = max ( dp [ k ][ j + 1 ], dp [ k + 1 ][ j ]);
}
}
op [ i ]. va = dp [ len ][ len ];
}
sort ( op , op + n , cmp );
for ( int i = 0 ; i < n ; i ++ ) {
printf ( "%s \n " , op [ i ]. str );
}
return 0 ;
}
// ,%%%%%%%%,
// ,%%/\%%%%/\%%
// ,%%%\c "" J/%%%
// %. %%%%/ o o \%%%
// `%%. %%%% _ |%%%
// `%% `%%%%(__Y__)%%'
// // ;%%%%`\-/%%%'
// (( / `%%%%%%%'
// \\ .' |
// \\ / \ | |
// \\/ ) | |
// \ /_ | |__
// (___________)))))))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值