[Ybtoj Advanced Advanced 2.1] [String] Same birthday

[Ybtoj Advanced Advanced 2.1] [String] Same birthday

topic

Insert picture description here
Insert picture description here


Problem-solving ideas

Quick sort, the
same output together
, skip the same without the same


Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct lzf {
    
    
    int r, y;
    string m;
} f[100200];
int n, p = 0;
bool cmp(lzf x, lzf y) {
    
    
    return ((x.y < y.y) || (x.y == y.y && x.r < y.r) ||
            (x.y == y.y && x.r == y.r && x.m.size() < y.m.size()) ||
            (x.y == y.y && x.r == y.r && x.m.size() == y.m.size() && x.m < y.m));
}
int main() {
    
    
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) 
        cin >> f[i].m >> f[i].y >> f[i].r;
    sort(f + 1, f + n + 1, cmp);
    int i = 0;
    while (i < n) {
    
    
        i++;
        if (f[i].y == f[i + 1].y && f[i].r == f[i + 1].r) 
        {
    
    
            printf("%d %d ", f[i].y, f[i].r);
            cout << f[i].m << " ";
            p = 1;
            while (f[i].y == f[i + 1].y && f[i].r == f[i + 1].r && i <= n) 
            {
    
    
                i++;
                cout << f[i].m << " ";
            }
            printf("\n");
        }
    }
    if (!p)
        printf("None\n");
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45621109/article/details/113358516