题解 | #找位置#

找位置

https://www.nowcoder.com/practice/e3b2cc44aa9b4851bdca89dd79c53150

//1.统计字母
//2.判断是否重复
//3.找到下一个重复项的位置
#include<iostream>
#include<algorithm>
#include<string>
#include<map>

using namespace std;

struct Mark {
    int count; //重复个数
    int flag;  //判断是否重复
};

map<char, Mark> M;

int main()
{
    string Str;
    int Next[1005];
    while (cin >> Str) {
        for (int i = 0; i < Str.size(); i++) {
            M[Str[i]].count++;
            M[Str[i]].flag++;
        }
        for (int i = 0; i < Str.size(); i++) {
            for (int j = i + 1; j < Str.size(); j++) {
                if (Str[i] == Str[j]) {
                    Next[i] = j; //记录下一个相同项位置。
                    break;
                }
            }
        }
        for (int i = 0; i < Str.size(); i++) {
            int temp = i;
            int flag = 0;
            while (M[Str[temp]].count && M[Str[temp]].flag >= 2) {
                cout << Str[temp] << ":" << temp;
                M[Str[temp]].count--;
                if (M[Str[temp]].count != 0) {
                    cout << ",";
                }
                temp = Next[temp];
                flag = 1; 
            }
            if (flag == 1) {
                cout << endl;
            }
        }
    }
}

#23届找工作求助阵地##上机考试#
全部评论

相关推荐

那一天的Java_Java起来:他本来公司就是做这个的,不就是正常的游戏客户端和服务器开发,软硬件联动,有啥恶心不恶心的,提前告诉你就是怕你接受不了,接受不了就没必要再往后走流程浪费时间,虽然这公司是一坨。
点赞 评论 收藏
分享
喜欢疯狂星期四的猫头鹰在研究求职打法:短作业优先
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客企业服务