zoukankan      html  css  js  c++  java
  • 彩色宝石项链

    有一条彩色宝石项链,是由很多种不同的宝石组成的,太多了,不说了。

    反正思路就是滑动窗口,用map记录,或者two pointer也行。上次我刚好在cf碰到一题,题意是求包含所有出现字符的最小子串,然后看到一种很好的解法,然后就写下来,这题算水题吧!还有,解决环的问题就是在后面再加上自己,就可以了。

     1 #include<bits/stdc++.h>
     2 #define pb push_back
     3 #define FOR(i, n) for (int i = 0; i < (int)n; ++i)
     4 #define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
     5 typedef long long ll;
     6 using namespace std;
     7 typedef pair<int, int> pii;
     8 const int maxn = 1e3 + 10;
     9 void solve() {
    10     string str;
    11     set<char> se;
    12     while(cin >> str) {
    13         se.clear();
    14         for (char x : str) {
    15             if(x >= 'A' && x <= 'E') se.insert(x);
    16         }
    17         if(se.size() < 5) {
    18             cout << 0 << endl; continue;
    19         }
    20         str += str;
    21         int n = str.size();
    22         vector<int> a(n, 0);
    23         for (int i = 0; i < 5; i++) {
    24             int last = -1;
    25             char cur = 'A' + i;
    26             for (int j = 0; j < n; j++) {
    27                 if(str[j] == cur) last = j;
    28                 if(last == -1) a[j] = n;
    29                 else a[j] = max(a[j], j - last + 1);
    30             }
    31         }
    32 
    33         int res = n;
    34         for (int i = 0; i < n; i++) {
    35             //cout << a[i] << endl;
    36             res = min(res, a[i]);
    37         }
    38         cout << n / 2 - res << endl;
    39     }
    40 }
    41 int main() {
    42     //freopen("test.in", "r", stdin);
    43     //freopen("test.out", "w", stdout);
    44     solve();
    45     return 0;
    46 }
  • 相关阅读:
    UVa 10010 Where's Waldorf?
    boost 学习笔记
    C++ enum类型的一个更好的用法
    新浪面试题:删除字符串中多余的空格
    微软面试题:写程序找出二叉树的深度
    c++中sizeof的分析
    复习计划
    boost学习之 时间和日期 timer
    c++ template学习总结3
    微软面试题:反序一个单向链表
  • 原文地址:https://www.cnblogs.com/y119777/p/5893163.html
Copyright © 2011-2022 走看看