[一天至少一题直到 ICPC 开赛 #018] 解题:Erase First or Second Letter(12/3

Erase First or Second Letter

题目连结
打题群组:
DC群组

最近几天一直很忙,像是礼拜四晚上被组员雷,弄了一整晚报告还要走路回家XD(公车没了)

解题

set的原理

insert的元素如果存在 ==> 不会插入(size不变)
insert的元素如果不存在 ==> 插入(size+1)

条件

只能删除左边的第一个或第二个

当有两个相同的字时的组合(举例 : aaa)
aaa aa a ==>也就是 有 n 个相同的字,组合的种类就是 n 个

当有不同的字的组合(举例 : abc)
a b c ab ac bc ==>6个,即 1 + 2 + 3

#include <iostream>#include <vector>#include <string>#include <set>using namespace std;int main(int argc, char const *argv[]){    int t;    cin >> t;    while (t--)    {        int n;        cin >> n;        set<char> check;        string s;        cin >> s;        long long ans = 0;        for (int i = 0; i < n; i++)        {            check.insert(s[i]);            ans = check.size() + ans;        }        cout << ans << endl;    }    return 0;}

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章