当前位置:网站首页>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;
}
边栏推荐
- Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance
- @Detailed explanation of valid annotation usage
- Advantages and disadvantages of using SNMP and WMI polling
- Analysis of common interview questions in redis
- Drosophila played VR and entered nature. It was found that there were attention mechanisms and working memory. The insect brain was no worse than that of mammals
- Laravel8+ wechat applet generates QR code
- JD 7 head search navigation layout
- How two hosts in different network segments directly connected communicate
- Tencent and China Mobile continued to buy back with large sums of money, and the leading Hong Kong stocks "led" the market to rebound?
- 这些老系统代码,是猪写的么?
猜你喜欢

How to use asemi FET 7n80 and how to use 7n80

TCP BBR as rate based
![[Suanli network] technological innovation of Suanli Network -- Key Technologies of green and security](/img/52/7dedc5b6e213839fbf5cee3963ac99.jpg)
[Suanli network] technological innovation of Suanli Network -- Key Technologies of green and security

What elements are indispensable for the development of the character? What are the stages

Cs5092 5V USB input boost two section lithium battery charging management IC, SOT23-6 miniature package

BGP - basic concept

Sophomores majoring in mechanics build a manipulator by hand -- full of compromise

IQ debugging of Hisilicon platform ISP and image (1)
![[200 opencv routines of youcans] 104 Motion blur degradation model](/img/a9/8841ffc8bd3c486bc4011a1a84ff45.jpg)
[200 opencv routines of youcans] 104 Motion blur degradation model

After unplugging the network cable, does the original TCP connection still exist?
随机推荐
Sleep quality today 67 points
PHP and WMI – explore windows with PHP
How to open an account online? Is it safe to open an account online?
An easy problem
How to deploy locally developed SAP ui5 applications to ABAP servers
Sophomores majoring in mechanics build a manipulator by hand -- full of compromise
Preliminary practice of niuke.com (summary)
Single lithium battery 3.7V power supply 2x12w stereo boost audio power amplifier IC combination solution
Rational investment and internationalism
Guess the size of the number
@Principle of preauthorize permission control
Global and Chinese gallium nitride (GAN) market output value scale forecast and application prospect analysis report 2022
Can TCP syn handshake messages transmit data
Why can't GC () free memory- Why does gc() not free memory?
The perfect presentation of Dao in the metauniverse, and platofarm creates a farm themed metauniverse
Metauniverse in 2022: robbing people, burning money and breaking through the experience boundary
[v2.0] automatic update system based on motion step API (support disconnection reconnection and data compensation)
3-7sql injection website instance step 3: attack type and attack strategy
CTFSHOW
Global and Chinese kaolin market operation scale and investment development proposal report 2022