统计单词中第一次出现两次的字母(C++)

it2025-01-19  30

代码(C++)

#include <iostream> #include <vector> #include <string> #define maxsize 50 using namespace std; struct hashMap { char key; int nums; }; struct HM { hashMap *data = new hashMap[maxsize]; int count; }; int getKeyIdx(char x, HM hm) { int i; for (i = 0; i < hm.count; ++i) { if (hm.data[i].key == x) { return i; } } return i; } void sta(string str) { HM hm; for (int i = 0; str[i] != '\0'; ++i) { int idx = getKeyIdx(str[i], hm); if (idx == hm.count) { ++hm.count; } hm.data[idx].key = str[i]; ++(hm.data[idx].nums); // cout<<hm.data[idx].key<<":"<<hm.data[idx].nums<<" "; if (hm.data[idx].nums == 2) { cout << hm.data[idx].key << " "; break; } } } void cal(vector<string> temp) { for (int i = 0; i < temp.size(); ++i) { sta(temp[i]); } } int main() { /* 3 farewell 20180901 TianJinUniversity */ int n; cin >> n; vector<string> temp; while (n--) { string str; cin >> str; temp.push_back(str); } cal(temp); return 0; }

结果

最新回复(0)