A 重新排列

题目地址:

https://ac.nowcoder.com/acm/contest/7509/A

基本思路:

尺取法,然后每次一下范围内的字母数量能否构成”puleyaknoi“就行了。

参考代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false); cin.tie(0)
#define int long long
#define ull unsigned long long
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define INF 0x3f3f3f3f

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

string mark = "puleyaknoi";
int memo[26],now[26];
string str;
bool check(){
  for(int i = 0; i < 26 ; i++) if(now[i] < memo[i]) return false;
  return true;
}
signed main() {
  IO;
  for (auto s : mark) memo[s - 'a']++;
  int t;
  cin >> t;
  while (t--){
    cin >> str;
    int n = SZ(str);
    str = ' ' + str;
    int r = 1,ans = INF;
    now[str[r] - 'a']++;
    for(int l = 1 ; l <= n ; l++) {
      while (!check() && r + 1 <= n) {
        now[str[++r] - 'a']++;
      }
      if (check()) ans = min(ans, r - l + 1);
      now[str[l] - 'a']--;
    }
    if(ans == INF) cout << -1 << '\n';
    else cout << ans << '\n';
  }
  return 0;
}