【CodeForces - 202A】LLPS (思维,字符串)
題干:
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
radarOutput
rrInput
bowwowwowOutput
wwwwwInput
codeforcesOutput
sInput
mississippOutput
ssssNote
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".
題目大意:
? ?給你一個(gè)字符串,讓你找到字典序最大的回文子序列。(給出了字典序的定義和子序列的定義)
解題報(bào)告:
? ?因?yàn)閿?shù)據(jù)量是10,本來想寫搜索,但是看了眼樣例,發(fā)現(xiàn)一個(gè)小規(guī)律,答案都是相同的字符,那么結(jié)論就出來了,,,找到ASCII最大的那個(gè)字符,輸出個(gè)數(shù)就可以了。
? 證明可以用樣例,,,你就算是選了一些比較大的回文串比如abzcczba,那么完全可以用其中最大的那個(gè)字符z來打頭。于是我們證明了一定用最大的那個(gè)字符打頭,那么我們假設(shè)字符串zaaz,那么完全可以直接取zz,于是得出結(jié)論。。
AC代碼:
#include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; //string s[5000],str; //int len; //int top; //bool fit(string ss) { // int l = ss.length(); // if(l == 0) return 0; // for(int i = 0; i<l/2; i++) { // if(ss[i] != ss[l-i]) return 0 ; // } // return 1; //} //void dfs(int cur,string tmp) { // if(cur >= len) return ; // if(fit(tmp)) s[++top] = tmp; // dfs(cur+1,tmp+str[cur+1]); // dfs(cur+1,tmp); //} int main() {string str;cin>>str;int len = str.length(); // string w = string(1,str[0]); // dfs(0,w); // printf("%d\n",top); // for(int i = 1; i<=top; i++) { // cout << s[i] << endl; // }char cur = 0;for(int i = 0; i<len; i++) {if(str[i] > cur) cur = str[i];}int cnt = 0;for(int i = 0; i<len; i++) {if(str[i] == cur) cnt++;}for(int i = 1; i<=cnt; i++) {putchar(cur);}return 0 ;}(話說代碼里那個(gè)搜索不太對(duì)好像,,,也沒debug)
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 202A】LLPS (思维,字符串)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: scureapp.exe - scure
- 下一篇: 【HDU - 1518】Square (