题解 | #找位置#

找位置

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届找工作求助阵地##上机考试#
全部评论

相关推荐

头像
04-17 09:29
已编辑
湖南农业大学 后端
睡姿决定发型丫:本硕末9也是0offer,简历挂了挺多,只有淘天 美团 中兴给了面试机会,淘天二面挂,美团一面kpi面,中兴一面感觉也大概率kpi(虽然国企,但一面0技术纯聊天有点离谱吧)
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客企业服务