对于字符串x和y, 如果擦除x中的某些字母(有可能全擦掉或者都不擦)能够得到y,我们就称y是x的子序列。例如."ncd"是"nowcoder"的子序列,而"xt"不是。
现在对于给定的一个字符串s,请计算出字典序最大的s的子序列。
输入包括一行,一个字符串s,字符串s长度length(1 ≤ length ≤ 50). s中每个字符都是小写字母
输出一个字符串,即字典序最大的s的子序列。
test
tt
这里我必须要说一个问题,字典序较大,就是两个字符串中比较大的元素,也就是java中compareTo的实现。好吧!我***了,哈哈哈。既然要找最的字典序,肯定要找开头最大。贪心算法,从后往前找,如果前者比后者大,则保留,否则就删除。
public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str = input.nextLine(); char[] res = str.toCharArray(); ArrayList<Character> arr = new ArrayList<>(); arr.add(res[res.length-1]); for (int i = res.length-1; i > 0; i--){ if ( arr.get(arr.size()-1) <= res[i-1]){ arr.add(res[i-1]); } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.size(); i++){ sb.append(arr.get(i)); } System.out.println(sb.reverse().toString()); } }
分析:从后面找单调递增的不连续序列
console.log(getSeq(line));
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(br.readLine().trim()); // 从后往前遍历,先把最后一个字符加入 char lastChar = sb.charAt(sb.length() - 1); int i = sb.length() - 2; while(i >= 0){ // 为了保证排在前面的字符ascii码更大,如果当前字符比后面的大就继续向前遍历 if(sb.charAt(i) >= lastChar){ lastChar = sb.charAt(i); i--; }else{ // 否则就把当前字符删掉 sb.deleteCharAt(i); } } System.out.println(sb); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String in = sc.next(); int n = in.length(); TreeMap<Character, Integer> treeMap = new TreeMap<>(new Comparator<Character>() { @Override public int compare(Character o1, Character o2) { return -o1.compareTo(o2); } }); char[] inC = in.toCharArray(); for (int i=0; i<in.length(); i++) { if (treeMap.containsKey(inC[i])) { treeMap.put(inC[i], treeMap.get(inC[i]) + 1); } else { treeMap.put(inC[i], 1); } } StringBuilder sb = new StringBuilder(); int index = 0; for (Map.Entry<Character, Integer> e: treeMap.entrySet()) { char c = e.getKey(); int v = e.getValue(); if (v == 0) { continue; } while (index < n && v > 0) { if (inC[index] == c) { v--; sb.append(c); } treeMap.put(inC[index], treeMap.get(inC[index]) -1); index++; } } System.out.println(sb.toString()); } }
publicclassMain {publicstaticvoidmain(String[] args) {Scanner input =newScanner(System.in);String str = input.nextLine();char[] res = str.toCharArray();ArrayList<Character> arr =newArrayList<>();arr.add(res[res.length-1]);for(inti = res.length-1; i >0; i--){if( arr.get(arr.size()-1) <= res[i-1]){arr.add(res[i-1]);}}StringBuilder sb =newStringBuilder();for(inti =0; i < arr.size(); i++){sb.append(arr.get(i));}System.out.println(sb.reverse().toString());}}
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author wylu */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); char[] s = br.readLine().toCharArray(); StringBuilder sb = new StringBuilder(); char ch = s[s.length - 1]; for (int i = s.length - 1; i >= 0; i--) { if (s[i] >= ch) { sb.append(s[i]); ch = s[i]; } } System.out.println(sb.reverse()); } }
#include <iostream> #include <stack> using namespace std; int main(){ string s; cin >> s; stack<char> sc; char min_c=*(s.rbegin()); for(auto it=s.rbegin();it!=s.rend();it++){ if((*it) >= min_c){ sc.push(*it); min_c=*it; } } while(!sc.empty()){ cout << sc.top(); sc.pop(); } return 0; }
#include <iostream> #include <vector> using namespace std; //最大递减子序列 int main() { string s,res; cin>>s; char c = s[s.length()-1]; res += c; for(int i=s.length()-2;i>=0;i--) { if(s[i]>=res[res.length()-1]) { res+=s[i]; } } for(int i=res.length()-1;i>=0;i--) { cout<<res[i]; } cout<<endl; }
class Solution: def solve(self, s): """思路:单调递减队列(允许重复)""" import collections queue = collections.deque() for char in s: if not queue: queue.append(char) continue while queue and queue[-1] < char: queue.pop() queue.append(char) return ''.join(queue) if __name__ == "__main__": so = Solution() s = input() print(so.solve(s)) def test(): so = Solution() assert so.solve("test") == "tt" assert so.solve("ababba") == "bbba"
#include<bits/stdc++.h> using namespace std; int main() { string s;cin>>s; string ans; int maxn=0; for(int i=s.length()-1;i>0;i--) if(s[i-1]<s[i]) s.erase(i-1,1); cout<<s<<endl; }从左往右麻烦的一 ----------- L->R(×),R->L(√).
#include <iostream> #include <string> #include <stack> using namespace std; int main() { string s; getline(cin, s, '\n'); int n = s.size(); if (n<2) cout << s << endl; stack<char> str; str.push(s[n-1]); for (int i=n-2; i>=0; --i) { if (s[i]>=str.top()) str.push(s[i]); } string res; while (!str.empty()) { res += str.top(); str.pop(); } cout << res << endl; return 0; }
s = input().strip() res = [] for c in s: while res and res[-1]<c: res.pop() res.append(c) print("".join(res))
/*** keep hungry and calm CoolGuang! ***/ #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") #pragma GCC optimize(3) #include <bits/stdc++.h> #include<stdio.h> #include<queue> #include<algorithm> #include<string.h> #include<iostream> #define debug(x) cout<<#x<<":"<<x<<endl; #define dl(x) printf("%lld\n",x); #define di(x) printf("%d\n",x); typedef long long ll; typedef unsigned long long ull; using namespace std; const ll INF= 1e17+7; const ll maxn = 2e5+700; const ll mod= 1e9+7; const double eps = 1e-9; const double PI = acos(-1.0); template<typename T>inline void read(T &a){char c=getchar();T x=0,f=1;while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;} ll n,m,p; char s[105]; int nex[105]; int main(){ scanf("%s",s+1); n = strlen(s+1); int pos = -1,mx = -1; for(int i=n;i>=0;i--){ nex[i] = pos; if(s[i]-'a'>= mx) pos = i,mx = s[i] - 'a'; } int top = 0; while(~nex[top]){ printf("%c",s[nex[top]]); top = nex[top]; } return 0; } /*** ***/