標籤: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 拼成最小的數 貪心排序