PIPIOJ 1463: PIPI的字符串问题IX set

54 篇文章 0 订阅

http://www.pipioj.online/problem.php?id=1463
在这里插入图片描述
思路:把字符串丢到 s e t set set里面即可,但是直接丢太暴力了。空间复杂度和时间复杂度都很高,我们可以把长度为 n n n的仅包含 c c c的字符串抽象为一对数据: ( c , n ) (c,n) (c,n),把这一对数据插入到 s e t set set中维护即可。

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;

const int maxn=1e5+5;

char s[maxn];

struct node
{
    char c;
    int cnt;
    node(char c,int cnt):c(c),cnt(cnt){}
    bool operator <(const node &a)const
    {
        if(c==a.c)
            return cnt<a.cnt;
        return c<a.c;
    }
};

void pr(const node &a)
{
    int cnt=a.cnt;
    while(cnt)
        putchar(a.c),--cnt;
    putchar('\n');
}

int main()
{
    while(~scanf("%s",s))
    {
        int n=strlen(s),ed;
        set<node> st;
        for(int i=0;i<n;)
        {
            ed=i+1;
            while(ed<n&&s[ed]==s[i])
                ed++;
            st.insert(node(s[i],ed-i));
            i=ed;
        }
        printf("%d\n",(int)st.size());
        for(const node& a:st)
            pr(a);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值