当前位置:网站首页>Acwing / 2004. Mauvaise écriture
Acwing / 2004. Mauvaise écriture
2022-06-25 06:36:00 【Alwaysdayone】


Explication du problème
Objet du sujet:
Conditions1:Pour tout préfixe de chaîne,Inclus dans ( Pas moins de ) Nombre de.
Conditions2:La chaîne saisie satisfait:Modifier un seul caractère au maximum,Peut devenir une chaîne de parenthèses équilibrée.
La question se limite donc à la nécessité de changer une parenthèse pour correspondre à la séquence!
Idées de conclusion:
Classification:1.Nombre de Parenthèses gauche et droiteR LMême chose.;2.La parenthèse droite doit être convertie en parenthèse gauche:R=L+2;La parenthèse gauche doit être convertie en parenthèse:L=R+2
Type①:Produits0Direct
Type②:Enregistrer de gauche à droite pour chaque emplacement Préfixes et,Par Condition1Oui.:.Rechercher de gauche à droite la position de la première parenthèse gauche qui est inférieure à la parenthèse droite, La position et l'une des parenthèses de droite précédentes sont changées en parenthèses de gauche pour satisfaire à la condition1,.Les réponses sont préfixées par des parenthèses droites à cet endroit et
Type③:Enregistrer de droite à gauche pour chaque emplacement Préfixes et,Par Condition1Oui.:Rechercher de droite à gauche la position de la première parenthèse droite qui est inférieure à la parenthèse gauche,Cette position et l'une des parenthèses d'ouverture à droite peuvent être changées en parenthèses d'ouverture pour satisfaire à la condition1,Les réponses sont préfixées par des parenthèses ouvertes à cet endroit et
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
string arr;
cin>>arr;
int n = arr.length();
int L = 0,R = 0;
for(int i = 0;i<n;i++){
if(arr[i] == '(') L++;
else R++;
}
int sumL[n+1] = {
0}; // Préfixe et
int sumR[n+1] = {
0};
if(L == R){
cout<<0<<endl;
}else if(L < R){
// La parenthèse droite doit être convertie en parenthèse gauche:R=L+2
for(int i = 0;i<n;i++){
sumL[i+1] = sumL[i];
sumR[i+1] = sumR[i];
if(arr[i] == '(') sumL[i+1] = sumL[i+1] + 1;
else sumR[i+1] = sumR[i+1] + 1;
// Jugement Position de la première parenthèse gauche inférieure à la parenthèse droite
if(arr[i] == ')' and sumL[i+1] < sumR[i+1]){
cout<<sumR[i+1]<<endl;
break;
}
}
}else{
// La parenthèse gauche doit être convertie en parenthèse:L=R+2
for(int i = n-1;i>=0;i--){
sumL[i] = sumL[i+1];
sumR[i] = sumR[i+1];
if(arr[i] == '(') sumL[i] = sumL[i] + 1;
else sumR[i] = sumR[i] + 1;
// Jugement La position de la première parenthèse droite est inférieure à celle de la parenthèse gauche
if(arr[i] == '(' and sumL[i] > sumR[i]){
cout<<sumL[i]<<endl;
break;
}
}
}
return 0;
}
边栏推荐
- Zhinai's database
- Is the number of indexes in a table the more the better?
- Rational investment and internationalism
- Laravel8+ wechat applet generates QR code
- Guess the size of the number
- At the age of 26, I was transferred to software testing with zero foundation. Now I have successfully entered the job with a monthly salary of 12K. However, no one understands my bitterness
- From file system to distributed file system
- 十大券商公司哪个佣金最低,最安全可靠?有知道的吗
- [hand torn STL] Stack & queue
- Record of friend guide
猜你喜欢

Es11 new methods: dynamic import(), bigint, globalthis, optional chain, and null value merging operator

From file system to distributed file system

What is VLAN
![[speech discrimination] discrimination of speech signals based on MATLAB double threshold method [including Matlab source code 1720]](/img/36/ad86f403b47731670879f01299b416.jpg)
[speech discrimination] discrimination of speech signals based on MATLAB double threshold method [including Matlab source code 1720]

Cannot activate inspection type when SAP retail uses transaction code mm41 to create commodity master data?

Exercise: completion

2022-02-19: fence installation. In a two-dimensional garden, there are some trees represented by (x, y) coordinates. As the installation cost is very expensive, your task is to enclose all the trees w

Meta universe is over, Web 3.0 will be fooled again?

With a younger brother OCR, say no to various types of verification codes!

BGP - basic concept
随机推荐
How do I turn off word wrap in iterm2- How to turn off word wrap in iTerm2?
Cs8126t 3.1w mono ultra low EMI unfiltered class D audio power amplifier IC
HCIP Day 16
[core content and derivation] the mystery of human memory system may be just like this
Ht7180 3.7V L 12v/2a built in MOS high current boost IC solution
Count the grid
十大券商公司哪个佣金最低,最安全可靠?有知道的吗
STL map的用法
[200 opencv routines of youcans] 104 Motion blur degradation model
What is the IP address
Global and Chinese gallium nitride (GAN) market output value scale forecast and application prospect analysis report 2022
Wechat applet authorization login + mobile phone sending verification code +jwt verification interface (laravel8+php)
Highway
Research Report on brand strategic management and marketing trends in the global and Chinese preserved fruit market 2022
How to create a handy vs Code?
Detailed explanation of @jsoninclude annotation in Jackson
Huawei machine test question: splicing URL
VMware virtual machine prompt: the virtual device ide1:0 cannot be connected because there is no corresponding device on the host.
Sword finger offer II 095 Longest common subsequence
JD 8 fleet stores search history, deletes history, clears history (not finished)