Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 – Elimination Round 2) A. Interview wi

来源:互联网 发布:淘宝网客户端 编辑:程序博客网 时间:2024/05/20 18:45

题意
给你一个字符串
里面有ogo ogogo ogogogo这样的要变成*
例如
7
aogogob
a***b
13
ogogmgogogogo
gmg
9
ogoogoogo


然后输出

如果有ogogo 也只输出 *
算法
这个其实想清楚了,就是一个区间覆盖问题,或者说更简单,我也不清楚
首先把区间找出来 就是有ogo的区间
不过好像现在想想 不可能会有重合的区间 更简单
不过我就是拿区间覆盖来做的

区间覆盖就是要排序 结束长的先放

所以首先就把这个区间找出来,然后排序,用pair来存 少写比较函数
然后就枚举每个字符的位置 如果在区间 就输出* 同时更新区间 否则就输出原来的字符

#define LOG(x) cout << #x << " = " << (x) << endl#define PRINTLN(x) cout << (x) << endl#define MEM(x, y) memset((x), (y), sizeof((x)))#include <bits/stdc++.h>using namespace std;const double PI = 2*acos(0);typedef long long ll;typedef complex<double> Complex;typedef pair<int, int> P;int nextInt(){  int x;  scanf("%d", &x);  return x;}ll nextLL(){  ll x;  scanf("%lld", &x);  return x;}//TEMPLATE//MAINint n;const int MAXN = 1000;char a[MAXN];vector<P> ps;void solve(){  for (int i = 0; i < n; i++) {    scanf("%c", &a[i]);  }  ps.clear();  for (int i = 0; i < n; i++) {    if (a[i] != 'o') continue;    int r = 0;    for (int j = 1; i + j + 1 < n; j += 2) {      if (i + j + 1 >= n) break;      if (!(a[i + j] == 'g' && a[i + j + 1] == 'o')) break;      r = i + j + 1;    }    if (r) {      ps.push_back(make_pair(r, i));    }  }  if (ps.empty()) {    for (int i = 0; i < n; i++) {      cout << a[i];    }    cout << endl;    return;  }  sort(ps.begin(), ps.end());  vector<P> ansP;  ansP.push_back(ps[0]); //可能访问无效内存  for (int i = 1; i < ps.size(); i++) {    if (ps[i].second > ps[i - 1].first) {      ansP.push_back(ps[i]);    }  }  int it = 0;  for (int i = 0; i < n; i++) {    if (it == ansP.size() || i < ansP[it].second) cout << a[i]; else {      cout << "***";      i = ansP[it++].first;    }  }  cout << endl;}int main(){  //freopen("in.txt", "r", stdin);  //freopen("out1.txt", "w", stdout);  while (scanf("%d\n", &n) != EOF) {    solve();  }}
0 0
原创粉丝点击