热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

牛客括号字符串的有效性和最长有效长度

题目链接:https:www.nowcoder.compractice335acafb6d5141b7873c4b0f24d53c57?tpId101&tqId331

题目链接:https://www.nowcoder.com/practice/335acafb6d5141b7873c4b0f24d53c57?tpId=101&tqId=33195&tPage=1&rp=1&ru=/ta/programmer-code-interview-guide&qru=/ta/programmer-code-interview-guide/question-ranking

题目大意:

  略。

分析:

略。

代码如下:

1 #include
2 using namespace std;
3
4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
5 #define Rep(i,n) for (int i &#61; 0; i <(int)(n); &#43;&#43;i)
6 #define For(i,s,t) for (int i &#61; (int)(s); i <&#61; (int)(t); &#43;&#43;i)
7 #define rFor(i,t,s) for (int i &#61; (int)(t); i >&#61; (int)(s); --i)
8 #define ForLL(i, s, t) for (LL i &#61; LL(s); i <&#61; LL(t); &#43;&#43;i)
9 #define rForLL(i, t, s) for (LL i &#61; LL(t); i >&#61; LL(s); --i)
10 #define foreach(i,c) for (__typeof(c.begin()) i &#61; c.begin(); i !&#61; c.end(); &#43;&#43;i)
11 #define rforeach(i,c) for (__typeof(c.rbegin()) i &#61; c.rbegin(); i !&#61; c.rend(); &#43;&#43;i)
12
13 #define pr(x) cout <<#x <<" &#61; " < 14 #define prln(x) cout <<#x <<" &#61; " < 15
16 #define LOWBIT(x) ((x)&(-x))
17
18 #define ALL(x) x.begin(),x.end()
19 #define INS(x) inserter(x,x.begin())
20 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
21 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
22 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
23 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
24
25 #define ms0(a) memset(a,0,sizeof(a))
26 #define msI(a) memset(a,0x3f,sizeof(a))
27 #define msM(a) memset(a,-1,sizeof(a))
28
29 #define MP make_pair
30 #define PB push_back
31 #define ft first
32 #define sd second
33
34 template
35 istream &operator>>(istream &in, pair &p) {
36 in >> p.first >> p.second;
37 return in;
38 }
39
40 template
41 istream &operator>>(istream &in, vector &v) {
42 for (auto &x: v)
43 in >> x;
44 return in;
45 }
46
47 template
48 ostream &operator<<(ostream &out, vector &v) {
49 Rep(i, v.size()) out <" \n"[i &#61;&#61; v.size() - 1];
50 return out;
51 }
52
53 template
54 ostream &operator<<(ostream &out, const std::pair &p) {
55 out <<"[" <", " <"]" <<"\n";
56 return out;
57 }
58
59 inline int gc(){
60 static const int BUF &#61; 1e7;
61 static char buf[BUF], *bg &#61; buf &#43; BUF, *ed &#61; bg;
62
63 if(bg &#61;&#61; ed) fread(bg &#61; buf, 1, BUF, stdin);
64 return *bg&#43;&#43;;
65 }
66
67 inline int ri(){
68 int x &#61; 0, f &#61; 1, c &#61; gc();
69 for(; c<48||c>57; f &#61; c&#61;&#61;&#39;-&#39;?-1:f, c&#61;gc());
70 for(; c>47&&c<58; x &#61; x*10 &#43; c - 48, c&#61;gc());
71 return x*f;
72 }
73
74 template<class T>
75 inline string toString(T x) {
76 ostringstream sout;
77 sout << x;
78 return sout.str();
79 }
80
81 inline int toInt(string s) {
82 int v;
83 istringstream sin(s);
84 sin >> v;
85 return v;
86 }
87
88 //min <&#61; aim <&#61; max
89 template
90 inline bool BETWEEN(const T aim, const T min, const T max) {
91 return min <&#61; aim && aim <&#61; max;
92 }
93
94 typedef unsigned int uI;
95 typedef long long LL;
96 typedef unsigned long long uLL;
97 typedef vector<int > VI;
98 typedef vector<bool > VB;
99 typedef vector<char > VC;
100 typedef vector<double > VD;
101 typedef vector<string > VS;
102 typedef vector VL;
103 typedef vector VVI;
104 typedef vector VVB;
105 typedef vector VVS;
106 typedef vector VVL;
107 typedef vector VVVI;
108 typedef vector VVVL;
109 typedef pair<int, int > PII;
110 typedef pair PLL;
111 typedef pair<int, string > PIS;
112 typedef pair<string, int > PSI;
113 typedef pair<string, string > PSS;
114 typedef pair<double, double > PDD;
115 typedef vector VPII;
116 typedef vector VPLL;
117 typedef vector VVPII;
118 typedef vector VVPLL;
119 typedef vector VVS;
120 typedef map<int, int > MII;
121 typedef unordered_map<int, int > uMII;
122 typedef map MLL;
123 typedef map<string, int > MSI;
124 typedef map<int, string > MIS;
125 typedef multiset<int > mSI;
126 typedef multiset<char > mSC;
127 typedef set<int > SI;
128 typedef stack<int > SKI;
129 typedef deque<int > DQI;
130 typedef queue<int > QI;
131 typedef priority_queue<int > PQIMax;
132 typedef priority_queue<int, VI, greater<int > > PQIMin;
133 const double EPS &#61; 1e-8;
134 const LL inf &#61; 0x7fffffff;
135 const LL infLL &#61; 0x7fffffffffffffffLL;
136 const LL mod &#61; 1e9 &#43; 7;
137 const int maxN &#61; 1e5 &#43; 7;
138 const LL ONE &#61; 1;
139 const LL evenBits &#61; 0xaaaaaaaaaaaaaaaa;
140 const LL oddBits &#61; 0x5555555555555555;
141
142 int N, M, ans;
143 string str;
144 // dp[i]表示str[0~i-1]以str[i - 1]结尾的最长有效长度
145 int dp[maxN];
146
147 int main() {
148 //freopen("MyOutput.txt","w",stdout);
149 //freopen("input.txt","r",stdin);
150 //INIT();
151 cin >> str;
152 N &#61; str.size();
153 dp[0] &#61; 0;
154
155 For(i, 1, N) {
156 if(str[i - 1] &#61;&#61; &#39;)&#39;) {
157 int tmp &#61; i - 2 - dp[i - 1];
158 if(tmp >&#61; 0 && str[tmp] &#61;&#61; &#39;(&#39;) {
159 dp[i] &#61; dp[i - 1] &#43; 2 &#43; dp[tmp];
160 }
161 }
162 ans &#61; max(ans, dp[i]);
163 }
164
165 cout < endl;
166 return 0;
167 }

View Code

 

转:https://www.cnblogs.com/zaq19970105/p/11418261.html



推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • 本文介绍了在mac环境下使用nginx配置nodejs代理服务器的步骤,包括安装nginx、创建目录和文件、配置代理的域名和日志记录等。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
author-avatar
wyf叶子_594
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有