使用C ++ STL的长度为2的不同连续子字符串的计数
在本教程中,我们将讨论一个使用C++STL计算长度为2的连续连续子字符串的程序。
为此,我们将提供一个字符串。我们的任务是计算和打印给定字符串中所有长度为2的唯一子字符串。
示例
#include<bits/stdc++.h>
using namespace std;
void calc_distinct(string str){
map<pair<char,char>, int> dPairs;
for (int i=0; i<str.size()-1; i++)
dPairs[make_pair(str[i], str[i+1])]++;
cout << "Distinct sub-strings with counts:\n";
for (auto it=dPairs.begin(); it!=dPairs.end(); it++)
cout << it->first.first << it->first.second << "-" << it->second << " ";
}
int main(){
string str = "abcacdcacabacaassddssklac";
calc_distinct(str);
return 0;
}输出结果
Distinct sub-strings with counts: aa-1 ab-2 ac-4 as-1 ba-1 bc-1 ca-4 cd-1 dc-1 dd-1 ds-1 kl-1 la-1 sd-1 sk-1 ss-2
热门推荐
10 师生聚餐的祝福语简短
11 求简短的职场祝福语
12 国庆求婚祝福语简短精辟
13 送个姐妹生日祝福语简短
14 生女儿的祝福语简短
15 给长辈祝福语简短大全
16 想念老公生日祝福语简短
17 简短祝福语大全暖心
18 端午老师祝福语简短优美