cf554A使用set和string解决字符串重复问题

本文探讨了字符串处理中字符去重的方法,并对比了使用C++标准库函数unique()与JAVA中HashSet自动去重功能的不同实现。通过两种方法的代码示例展示了如何计算字符串在插入特定字符后的不同组合数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这道题当时自己A的,比较麻烦,而且想了好久,还用了unique()函数去重,比较麻烦,而且WA了一发

自己的代码:

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cctype>
#include<cassert>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<cstdlib>
using namespace std;
#define LL long long
int main()
{
   char s[25],b[25];
   while(gets(s))
   {
       int l=strlen(s);
       memcpy(b,s,sizeof(s));
       sort(b,b+l);
       int cc=unique(b,b+l)-b;
       int num=(26-cc)*(l+1);
       num+=cc*l;
       num=num-(l-cc);
       cout<<num<<endl;
   }
    return 0;
}
看了题解之后,题解使用了JAVA中的hashset和string写的,使用了hashset的自动去重功能,直接在使用string连接暴力一发由于自己对JAVA不太熟悉,手打了一发set和string写的,比较简单,代码:


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<string>
using namespace std;
int main()
{
    string s;
	while(cin>>s)
    {
        set<string> a;
        for(int i=0;i<=s.length();i++)
            for(char c='a';c<='z';c++)
        {
            a.insert(s.substr(0,i)+c+s.substr(i));
        }
        cout<<a.size()<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值