牛客网 KY228 找位置
发布日期:2021-07-22 07:29:03 浏览次数:4 分类:技术文章

本文共 883 字,大约阅读时间需要 2 分钟。

思路

利用map将位置与字符对应,然后根据其出现顺序进行查找,其中关键一步是按照value去查找key值,将多次出现的字符位置进行记录后再输出。

代码如下

#include 
#include
#include
#include
#include
using namespace std;int main(){
string input; while (cin >> input) {
vector
s; //存放字符出现的顺序 map
position; //以位置为key,字符为value的map for (int i = 0; i < input.length(); ++i) { //构建map auto k = find(s.begin(), s.end(), input[i]); if (k == s.end()) s.push_back(input[i]); position.insert(pair
(i, input[i])); } for (int i = 0; i < s.size(); ++i) { vector
result; //记录重复字符出现位置 for (auto t = position.begin(); t != position.end(); ++t) { //按照value查找key值 if (t->second == s[i]) result.push_back(t->first); } if (result.size() > 1) { for (int j = 0; j < result.size(); ++j) { if (j != result.size() - 1) cout << s[i] << ":" << result[j] << ","; else cout << s[i] << ":" << result[j]; } cout << endl; } } } }

转载地址:https://blog.csdn.net/m0_45689014/article/details/114107999 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:递归 N皇后问题 学习笔记
下一篇:牛客网 KY117 奥运排序问题

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月02日 15时47分18秒