使用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
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬