题解 | #找位置#

找位置

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

相关推荐

06-18 15:03
门头沟学院 Java
至少实习看起来比去年好?问了下群里的同学和身边的同学,人均有offer。有的还有好几个大厂offer
菜鸟1973:上一年暑期也是人均大厂实习offer,结果秋招跟不招人一样,大部分都转正了
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务