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;}