ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมที่จะนับสตริงย่อยที่ต่อเนื่องกันซึ่งมีความยาวสองสตริงโดยใช้ C++ STL
สำหรับสิ่งนี้เราจะจัดให้มีสตริง งานของเราคือนับและพิมพ์สตริงย่อยที่ไม่ซ้ำกันทั้งหมดที่มีความยาวสองจากสตริงที่กำหนด
ตัวอย่าง
#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