使用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
廣告
資料結構
聯網
RDBMS
作業系統
爪哇
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP