随手练——字符串按最小(大)字典序拼接
生活随笔
收集整理的這篇文章主要介紹了
随手练——字符串按最小(大)字典序拼接
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一般想到的,將所有字符串是按字典序從小到大依次排下去。
但其實(shí)是錯的,比如 b , ba按這個思想排出來,是 bba,但其實(shí)最小的是bab,就要改一下比較策略了,改成拼接之后,比較誰小。
int cmp(string s1,string s2) {return s1 + s2 < s2 + s1; }?
https://www.luogu.org/problemnew/show/P1012。
比如這個,給一堆數(shù),拼出來一個最大的,比如321,32;會拼成32132,不是最大的,最大的是32321。
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std;bool cmp(string s1, string s2) {return s1 + s2 > s2 + s1; }int main() {int n;cin >> n;vector<string>v;while (n--) {string s;cin >> s;v.push_back(s);}sort(v.begin(), v.end(), cmp);for (int i = 0; i < v.size(); i++)cout << v[i];cout << endl;return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/czc1999/p/10355969.html
總結(jié)
以上是生活随笔為你收集整理的随手练——字符串按最小(大)字典序拼接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HNU暑假程序设计训练 0419
- 下一篇: 《先知·工作》