夕日的光辉
#include <stdio.h> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #pragma GCC optimize(2) #define mm(i,v) memset(i,v,sizeof i); #define mp(a, b) make_pair(a, b) #define one first #define two second using namespace std; typedef long long ll; typedef pair<int, int > PII; const int N = 1e6 + 5, mod = 1e9 + 9, INF = 0x3f3f3f3f; int t, n, p1, p2, p3, p4, flag; char s[N]; int main() { cin >> t; while (t--) { cin >> n; scanf("%s", s + 1); p1 = 0, p2 = 0, p3 = 0, p4 = 0; for (int i = 1; i <= n; ++i) { if (s[i] == 'p') p1 = 1; if (s[i] == 'i' && p1 != 0) p2 = 1; if (s[i] == 'n' && p2 != 0) p3 = 1; if (s[i] == 'k' && p3 != 0) { p4 = 1; break; } } if (p4 == 0) { puts("-1"); continue; } flag = 0; for (int i = 1; i <= n; ++i) { if (flag == 0) { if (s[i] == 'p') { p1 = i; flag = 1; continue; } else { s[i] = 'a'; continue; } } else if (flag == 1) { if (s[i] == 'i') { p2 = i; break; } else { s[i] = 'a'; continue; } } } flag = 0; for (int i = n; i >= 1; --i) { if (flag == 0) { if (s[i] == 'k') { p4 = i; flag = 1; continue; } else { s[i] = 'a'; continue; } } else if (flag == 1) { if (s[i] == 'n') { p3 = i; break; } else { s[i] = 'a'; continue; } } } int Max = p3 - p2 - 1; // cout << p3 << " " << p2 << endl; for (int i = p3 - 1; i >= 1; --i) { if (s[i] == 'i') { if (i - p1 - 1 > Max) { Max = i - p1 - 1; } break; } } for (int i = p2 + 1; i <= n; ++i) { if (s[i] == 'n') { if (p4 - i - 1 > Max) { Max = p4 - i - 1; } break; } } cout << Max << endl; } } /* 1 22 pinpipkinpkinpknninnpk */