1097 拼成最小的數 貪心排序

來源:互聯網
上載者:User

標籤:printf   貪心   fine   style   htm   ide   algo   define   vector   

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1097

2

280

28023

 

2

280

2802

 

2

32

323231

 

首先,起始位比較小的,肯定排在前面的了。然後關鍵就是如何處理相同的時候,也就是32和321怎麼比較。

我的做法:如果比較完後,較短的那個重頭開始比較。相當於拼接了上去迴圈

我想到怎樣做的來源是:

它肯定是開頭相同的一堆放在一起的,比如開頭是1的有n個,肯定是前n個開頭都是1的那堆。因為這樣結果更優。

那麼他們兩兩比較時,如果有一個沒了,那麼就由開頭的補充,然後wa後,找資料,就這樣做了。

 

#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <assert.h>#define IOS ios::sync_with_stdio(false)using namespace std;#define inf (0x3f3f3f3f)typedef long long int LL;#include <iostream>#include <sstream>#include <vector>#include <set>#include <map>#include <queue>#include <string>#include <bitset>struct node {    char str[22];    int lenstr;    bool operator < (const struct node & rhs) const {        int now = 1;        if (lenstr < rhs.lenstr) {            for (int i = 1; i <= rhs.lenstr; ++i) {                if (i > lenstr) {                    if (str[now] != rhs.str[i]) return str[now] < rhs.str[i];                    now++;                    if (now > lenstr) now = 1;                } else {                    if (str[i] != rhs.str[i]) return str[i] < rhs.str[i];                }            }            return str[now] < rhs.str[1];        } else {            for (int i = 1; i <= lenstr; ++i) {                if (i > rhs.lenstr) {                    if (str[i] != rhs.str[now]) return str[i] < rhs.str[now];                    now++;                    if (now > rhs.lenstr) now = 1;                } else {                    if (str[i] != rhs.str[i]) return str[i] < rhs.str[i];                }            }            return str[1] < rhs.str[now];        }        return false;    }}a[11111];const int maxn = 1e6 + 20;char ans[maxn];void work() {    int n;    scanf("%d", &n);    for (int i = 1; i <= n; ++i) {        scanf("%s", a[i].str + 1);        a[i].lenstr = strlen(a[i].str + 1);    }    sort(a + 1, a + 1 + n);    int lenans = 0;    for (int i = 1; i <= n; ++i) {        strcpy(ans + lenans + 1, a[i].str + 1);        lenans += a[i].lenstr;    }    for (int i = 1; i <= lenans; ++i) {        printf("%c", ans[i]);        if (i % 1000 == 0) {            printf("\n");        }    }}int main() {#ifdef local    freopen("data.txt", "r", stdin);//    freopen("data.txt", "w", stdout);#endif    work();    return 0;}
View Code

 

1097 拼成最小的數 貪心排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.