using namespace std; const int N=105; int n,d[N][N]; string s; char a[N]; bool ok(char l,char r){ return l=='('&&r==')'||l=='['&&r==']'; } int main(){ while(cin>>s&&s!="end"){ n=s.size(); for(int i=1;i<=n;i++) a[i]=s[i-1]; memset(d,0,sizeof(d)); for(int len=2;len<=n;len++) for(int i=1;i<=n-len+1;i++){ int j=i+len-1; if(ok(a[i],a[j])) d[i][j]=d[i+1][j-1]+2; for(int k=i;k<j;k++) d[i][j]=max(d[i][j],d[i][k]+d[k+1][j]); } cout<<d[1][n]<<endl; } return 0; }
时间: 2024-04-17 14:23:51 浏览: 17
这段代码是一个动态规划算法的实现,用于求解最长合法括号序列的长度。
首先,使用了命名空间std,并定义了常量N作为数组的大小,声明了整数变量n和二维数组d。还有一个字符串变量s和字符数组a,用于存储输入的括号序列。
接下来,定义了一个函数ok,用于判断两个字符是否为合法的括号对,即'('和')'或'['和']'。
在主函数中,使用while循环不断从输入流(cin)中读取括号序列字符串,并判断是否为"end",如果是则结束循环。
对于每个括号序列,首先计算其长度n,并将其转换为字符数组a。
然后,使用动态规划的方式计算最长合法括号序列的长度。首先初始化d数组,将长度为1的子序列的值设为0。
接着,使用两层循环遍历所有可能的子序列长度和起始位置。对于每个子序列,判断首尾字符是否为合法的括号对,如果是,则当前子序列的最长合法括号序列长度为d[i+1][j-1]+2(d[i+1][j-1]表示去掉首尾字符后的子序列的最长合法括号序列长度,加上首尾字符本身)。然后,再遍历子序列中的每个位置k,将子序列分成两部分,分别计算两部分的最长合法括号序列长度,并取两者之和的最大值作为当前子序列的最长合法括号序列长度。
最后,输出d[1][n]即为整个括号序列的最长合法括号序列的长度。
请注意,这段代码没有包含完整的头文件,如果要编译运行需要补充相应的代码。另外,在使用memset函数时,需要包含头文件<cstring>。
相关问题
#include <iostream> using namespace std; class Student { string name; float score; static int total; int Student (const char n, float s) { name = n; total += s; return 0; } }; int main() { int
这个程序有几个错误:
1. 在类Student中的构造函数应该是public而不是private。
2. 构造函数的参数应该是string类型而不是const char类型。
3. total是一个静态变量,应该在类外进行初始化和定义。
4. 在构造函数中,total应该累加的是1而不是分数s。
改正后的程序如下所示:
```
#include <iostream>
using namespace std;
class Student {
private:
string name;
float score;
static int total;
public:
Student(string n, float s) {
name = n;
score = s;
total++;
}
static void showTotal() {
cout << "Total: " << total << endl;
}
};
int Student::total = 0;
int main() {
Student s1("Tom", 90);
Student s2("Jerry", 85);
Student s3("Lucy", 95);
Student::showTotal();
return 0;
}
```
这个程序中,构造函数已经声明为public,同时构造函数的参数也已经改为了string类型。total被定义为一个静态变量,并在类外进行了初始化。在构造函数中,total累加的是1而不是分数s。最后在main函数中调用了静态函数showTotal()来输出总人数。
将代码的gets方法改为getchar方法:#include<iostream> #include<string> #include<bits/stdc++.h> using namespace std; const int N = 1e6; int trie[N + 50][26]; int num[N + 50] = { 0 }; int pos = 1; void Insert(char str[]) { int index = 0; for (int i = 0; str[i]; i++) { int n = str[i] - 'a'; if (trie[index][n] == 0) trie[index][n] = pos++; index = trie[index][n]; num[index]++; } } int Find(char str[]) { int index = 0; for (int i = 0; str[i]; i++) { int n = str[i] - 'a'; if (trie[index][n] == 0) return 0; index = trie[index][n]; } return num[index]; } int main() { char str[11]; while (gets(str)) { if (!strlen(str)) break; Insert(str); } while (gets(str)) { int res = Find(str); if (res > 0) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
好的,代码的修改如下:
```c++
#include<iostream>
#include<string>
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6;
int trie[N + 50][26];
int num[N + 50] = { 0 };
int pos = 1;
void Insert(char str[]) {
int index = 0;
for (int i = 0; str[i]; i++) {
int n = str[i] - 'a';
if (trie[index][n] == 0)
trie[index][n] = pos++;
index = trie[index][n];
num[index]++;
}
}
int Find(char str[]) {
int index = 0;
for (int i = 0; str[i]; i++) {
int n = str[i] - 'a';
if (trie[index][n] == 0)
return 0;
index = trie[index][n];
}
return num[index];
}
int main() {
char str[11];
int i = 0;
while ((str[i] = getchar()) != '\n') { // 读入一行
i++;
}
str[i] = '\0';
i = 0;
while (str[i] != '\0') { // 处理每个单词
int j = i;
while (str[j] != ' ' && str[j] != '\0') {
j++;
}
char word[11];
int k = 0;
for (int p = i; p < j; p++) {
word[k++] = str[p];
}
word[k] = '\0';
Insert(word);
i = j + 1;
}
while ((str[i] = getchar()) != EOF) { // 读入查询
if (str[i] == '\n') {
int res = Find(str);
if (res > 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
i = 0;
} else {
i++;
}
}
return 0;
}
```
修改思路:首先需要读入一行字符串,然后逐个处理每个单词,插入到 Trie 树中;之后每次读入查询时,读入一行,然后对每个单词查询 Trie 树,并输出结果。在这个过程中,需要注意每个单词之间的分割符号。