当前位置:网站首页>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;
}
边栏推荐
- [short time average zero crossing rate] short time average zero crossing rate of speech signal based on MATLAB [including Matlab source code 1721]
- Talk about TCP and UDP
- Go language library management restful API development practice
- Highway
- Research Report on marketing channel analysis and competitive strategy of China's polycarbonate industry 2022
- In depth inventory: 23 vscode plug-in artifacts that improve development efficiency and aesthetics
- Brief introduction and use of JSON
- Cannot activate inspection type when SAP retail uses transaction code mm41 to create commodity master data?
- Sophomores majoring in mechanics build a manipulator by hand -- full of compromise
- You can see the classification of SQL injection. SQL injection point /sql injection type /sql injection has several /sql injection point classifications
猜你喜欢
Zero foundation wants to learn web security, how to get started?
Vegetables sklearn - xgboost (2)
Cve-2022-23131 - bypass SAML SSO authentication
Sleep quality today 67 points
Gb28181 protocol -- timing
DNS domain name system
How two hosts in different network segments directly connected communicate
From file system to distributed file system
Pre knowledge of asynchronous operation
JS dynamic table creation
随机推荐
TCP BBR as rate based
@Detailed explanation of valid annotation usage
Viewing Chinese science and technology from the Winter Olympics (V): the Internet of things
[no title] dream notes 2022-02-20
Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance
Analysis report on production and sales demand and sales prospect of global and Chinese phosphating solution Market 2022-2028
Socket, network model notes
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
Gb28181 protocol -- timing
十大券商公司哪个佣金最低,最安全可靠?有知道的吗
The five minute demonstration "teaches" actors to speak foreign languages and can seamlessly switch languages. This AI dubbing company has just received a round a financing of 20million US dollars
SAP QM executes the transaction code qp01, and the system reports an error -material type food is not defined for task list type Q-
Analysis of common interview questions in redis
An interview question record about where in MySQL
What is the IP address
Research Report on marketing channel analysis and competitive strategy of China's polycarbonate industry 2022
Vegetables sklearn - xgboost (2)
Query process of MySQL secondary index
The sum problem
A + B Again