SCAU2020春季个人排位赛div2 #4 B题

#题目原题
This problem’s actual name, “Lexicographically Largest Palindromic Subsequence” is too long to fit into the page headline.

You are given string s consisting of lowercase English letters only. Find its lexicographically largest palindromic subsequence.

We’ll call a non-empty string s[p1p2… pk] = sp1sp2… spk (1  ≤  p1 < p2 < … < pk  ≤  |s|) a subsequence of string s = s1s2… s|s|, where |s| is the length of string s. For example, strings “abcb”, “b” and “abacaba” are subsequences of string “abacaba”.

String x = x1x2… x|x| is lexicographically larger than string y = y1y2… y|y| if either |x| > |y| and x1 = y1, x2 = y2, …, x|y| = y|y|, or there exists such number r (r < |x|, r < |y|) that x1 = y1, x2 = y2, …, xr = yr and xr  +  1 > yr  +  1. Characters in the strings are compared according to their ASCII codes. For example, string “ranger” is lexicographically larger than string “racecar” and string “poster” is lexicographically larger than string “post”.

String s = s1s2… s|s| is a palindrome if it matches string rev(s) = s|s|s|s| - 1… s1. In other words, a string is a palindrome if it reads the same way from left to right and from right to left. For example, palindromic strings are “racecar”, “refer” and “z”.

Input
The only input line contains a non-empty string s consisting of lowercase English letters only. Its length does not exceed 10.

Output
Print the lexicographically largest palindromic subsequence of string s.

Examples
Input
radar
Output
rr
Input
bowwowwow
Output
wwwww
Input
codeforces
Output
s
Input
mississipp
Output``
ssss
Note
Among all distinct subsequences of string “radar” the following ones are palindromes: “a”, “d”, “r”, “aa”, “rr”, “ada”, “rar”, “rdr”, “raar” and “radar”. The lexicographically largest of them is “rr”.

##题目大意:
给定一个字符串,求按字典顺序排序的最大非递增回文串

##思路:
由于有最大非递增这个特点,可以考虑用单调栈求出最大非递增串,之后再判断是否为回文串,若不是,则删除最后一个字符再判断
##AC代码:

#include <bits/stdc++.h>
using namespace std;

bool is_hui(char *t)//判断是否为回文串的函数
{
	int len = strlen(t);
	bool flag = true;
	for(int i = 0; i < len/2; i++)
	{
		if(t[i] != t[len-1-i])
		{
			flag = 0;
			break;
		}
	}
	return flag;
}

int main()
{
	stack<int> st;
	char s[20];
	cin>>s;
	int len = strlen(s);
	for(int i = 0; i < len; i++)//单调栈维护
	{
		if(st.empty()||s[i] <= s[st.top()])
			st.push(i);
		else
		{
			while(!st.empty()&&s[i] > s[st.top()])
			{
				st.pop();
			}
			st.push(i);
		}
	}
	char tmp[20];
	int k = st.size()-1;
	int a = st.size();
	while(st.size())
	{
		int top = st.top();
		st.pop();
		tmp[k--] = s[top];
	}
	tmp[a] = '\0';
	//cout<<tmp<<endl;
	while(!is_hui(tmp))
	{
		tmp[--a] = '\0';
	}
	cout<<tmp<<endl;
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值