题解 | #找位置#
找位置
http://www.nowcoder.com/practice/e3b2cc44aa9b4851bdca89dd79c53150
时间复杂度为 O(n)
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string s;
while (cin >> s)
{
vector <vector <int >> v(128);
for (int i = 0; i < s.size(); i++)
{
v[s[i]].push_back(i);
}
for (int i = 0; i < s.size(); i++)
{
char ch = s[i];
if (v[ch].size() > 1)
{
for(int j = 0; j < v[ch].size(); j++)
{
cout << s[v[ch][j]] << ":" <<v[ch][j];
if(j != v[ch].size() - 1) cout << ",";
else cout << endl;
}
v[ch].clear();
}
}
}
}