当前位置:网站首页>Niuke rearrangement rule taking method
Niuke rearrangement rule taking method
2022-06-30 09:18:00 【Young white horse】
Title Description
Niuniu has a favorite string ”puleyaknoi“.
Cattle have T A very long string , He likes to put substrings in strings ( A continuous segment ) Upset , And rearrange it to your liking .
If Niuniu can rearrange a paragraph into a string he likes , He would call this substring : Favorite substring .
Niuniu is a lazy man , He doesn't like to rearrange too long substrings , Then he will feel tired with his glasses .
You can help him find out for each string , What is the length of the shortest favorite substring ?
without , Please export -1.
Input description :
The first line represents the number of data groups, and the next line contains a string ( Ensure that the string contains only lowercase letters )
Output description :
common T One answer per line
Example 1
Input
2
sxpuleyaaknoip
konijiwa
Output
11
-1
explain
sxpuleyaaknoip in puleyaaknoi Can be rearranged into puleyaknoia, It includes puleyaknoi.
konijiwa Cannot rearrange puleyaknoi, So it is -1
remarks :
The string length does not exceed 1e5
Reference link luoyongjun's blog Ruler method
Ruler method ( Double pointer )
Topic analysis : When I first saw this topic , It feels very difficult to do , The only way to think about it is violence , But it's clear that violence will definitely go out of time , A look at the problem solution shows that it is a classic example of the rule taking method , But this kind of topic has been done less , It took some time to understand the code , I also refer to the blog to understand the concept and usage of ruler taking method . See code for specific topic analysis
#include <bits/stdc++.h>
using namespace std;
string s = "puleyaknoi";
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int a[maxn], b[maxn];
char c[maxn];
int solve() // Judgment function , Judge whether the number of elements meets the requirements of the topic
{
for (int i = 0; i < 26; ++i)
if (a[i] < b[i])
return false;
return true;
}
int main()
{
int t;
scanf("%d", &t);
for (int i = 0; i < s.size(); ++i)
b[s[i] - 'a']++; // Record the number of each element of the target substring
getchar(); // Get carriage return , Prevent interference with the input of subsequent strings
for (int i = 1; i <= t; ++i)
{
scanf("%s", c);
int lc = strlen(c);
int r = 0, ans = inf;
for (int l = 0; l < lc; ++l)
{
while (!solve() && r < lc) // Find the target substring ,r Move right until the current interval [l,r] The target substring appears in
a[c[r++] - 'a']++;
if (solve()) // Once the target substring appears, record the minimum interval
ans = min(ans, r - l);
a[c[l] - 'a']--; //l Constantly moving right , Remove one leftmost element each time , The goal is to find the minimum interval
}
if (ans == inf) // Can't find
puts("-1");
else
printf("%d\n", ans);
}
return 0;
}
The Mid Autumn Festival is the national day , A good home meets a prosperous country |
---|
边栏推荐
- Esp32 things (VIII): music playing function of function development
- Rew acoustic test (IV): test principle of rew
- What are the SQL add / delete / modify queries?
- Maxiouassigner of mmdet line by line interpretation
- Use V-IF with V-for
- C # get the current timestamp
- 2020-11-02
- Wikimedia Foundation announces the first customers of its new commercial product "Wikimedia enterprise"
- Linear-gradient()
- Find the number that appears only once in the array
猜你喜欢
Interpretation of orientedrcnn papers
维基媒体基金会公布新商业产品“维基媒体企业”首批客户
Opencv learning notes-day14 drawing of image geometry (rect class rotatedrect class, rectangle drawing rectangle circle drawing circular function line drawing line function ellipse drawing elliptic fu
Applet learning path 2 - event binding
Flutter 0001, environment configuration
9.JNI_ Necessary optimization design
Opencv learning notes -day4 image pixel reading and writing operations (array traversal and pointer traversal implementation, uchar vec3b data type and mat class functions mat:: at(), mat:: ptr())
Deeply understand the working principle of kotlin collaboration suspend (beginners can also understand it)
100 lines of code and a voice conversation assistant
将线程绑定在某个具体的CPU逻辑内核上运行
随机推荐
Installation, use and explanation of vulnerability scanning tool OpenVAS
Qt连接神通数据库
What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?
JPA naming rules
Applet learning path 2 - event binding
[paid promotion] collection of frequently asked questions, FAQ of recommended list
Opencv learning notes -day 12 (ROI region extraction and inrange() function operation)
Advanced technology management -- how managers design and build echelons
Opencv learning notes -day2 (implemented by the color space conversion function cvtcolar(), and imwrite image saving function imwrite())
5. Messager framework and imessager interface
Agp7.0|kts makes a reinforced plug-in
7. know JNI and NDK
ES6 learning path (III) deconstruction assignment
Opencv learning notes-day9 opencv's own color table operation (colormap coloraptypes enumeration data types and applycolormap() pseudo color function)
Linear-gradient()
Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function
Opencv learning notes -day4 image pixel reading and writing operations (array traversal and pointer traversal implementation, uchar vec3b data type and mat class functions mat:: at(), mat:: ptr())
Duplicate entry '2' for key 'primary appears in JPA‘
Dart asynchronous task
Pytorch BERT