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__)%%'
// // ;%%%%`\-/%%%'
// (( / `%%%%%%%'
// \\ .' |
// \\ / \ | |
// \\/ ) | |
// \ /_ | |__
// (___________)))))))