c++程序or 文本 是否括号匹配, 不匹配输出第一个不匹配的符号?
- 对于c++程序类文本 核心:对于每一个右括号发,肯定和离他最近的左边括号匹配,如果离他最近的左括号不是与之对应左括号,或者直接没有左括号。那就的就肯定不匹配。 开一个储存左括号的栈
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin.tie(nullptr);
getline(cin, s);
m['$'] = '@';
m[')'] = '(';
m[']'] = '[';
m['}'] = '{';
while ( s[0] != '.' ) {
for ( int i = 0; i < s.length(); i++ ) {
char c = s[i];
if ( i + 1 < s.length() && c == '/' && s[i + 1] == '*' ) {
i++;
c = '@';
}
else if ( i + 1 < s.length() && c == '*' && s[i + 1] == '/' ) {
i++;
c = '$';
}
if ( m.count(c) ) {
if ( das.empty() ) {
printf("NO\n");
if ( c == '$' )
cout << "?-*/" << endl;
else
cout << "?-" << c << endl;
return 0;
}
else if ( das.top() != m[c] ) {
printf("NO\n");
if ( das.top() == '@' )
cout << "/*-?" << endl;
else
cout << das.top() << "-?" << endl;
return 0;
}
das.pop();
}
else {
if ( c == '(' || c == '{' || c == '[' || c == '@' ) {
das.push(c);
}
}
}
getline(cin, s);
}
if ( das.empty() ) {
printf("YES");
}
else {
printf("NO\n");
if ( das.top() == '@' )
cout << "/*-?" << endl;
else
cout << das.top() << "-?" << endl;
}
return 0;
}
对于普通类文本:符号匹配思想差不多,但是要记录第一个不匹配的字符位置,有几个特殊样例
012345
((({)} 第一次不匹配的位置应该是4 (,并且对于普通文本{}这个括号她是匹配的,
但是在c++程序文本那肯定是不匹配的。
012345
((({} 第一次不匹配的位置应该是0 (
012345678
(((())))) 第一次不匹配的位置7
code
string s;
int cnt[6];
size_t pos = 1000;//right more,pos
char ans = ' ';
bool once = 1;
size_t apos = 2000;
int rev[6];
map<char, stack<char>> dty;
map<char, char> sav;
void del()
{
int len = s.size() - 1;
ifor(i, 0, len)
{
if ( s[i] == '/' && s[i + 1] == '*' )
{
s[i] = '@';
s[i + 1] = ' ';
}
if ( s[i] == '*' && s[i + 1] == '/' )
{
s[i] = '$';
s[i + 1] = ' ';
}
}
// cout << s << endl;
}
const string ss1 = "@({[";
const string ss2 = "$]})";
void pro(char c, int p) {
if ( ss1.find(c) != string::npos )
{
dty[c].push(c);
}
else if ( ss2.find(c) != string::npos )
{
if ( dty[sav[c]].empty() )
{
if ( once )
{
ans = c;
pos = p;
}
}
else {
dty[sav[c]].pop();
}
}
}
vector<char> res;
void solved()
{
ifor(i, 0, s.size() - 1)
{
pro(s[i], i);
}
for ( auto it = dty.begin(); it != dty.end(); ++it )
{
if ( !(it->second).empty() )
{
res.push_back(sav[it->first]);
}
}
auto p = begin(res);
// cus::print(res);
// cout << endl;
if ( res.empty() );
else {
size_t temp;
while ( p != res.end() )
{
if ( s.rfind(*p) != string::npos )
temp = s.rfind(*p);
else temp = s.find(sav[*p]);
apos = min(apos, temp);
++p;
}
}
int flag = 1;
if ( apos > pos ) {
flag = 0; apos = pos;
}
if ( pos >= s.size() && apos >= s.size() ) {
cout << "YES"; return;
}
else {
cout << "NO" << endl;
if ( flag )//left more;
{
if ( s[apos] == '@' || s[apos] == '$' )
cout << "/*-?" << endl;
if ( s[apos] == '(' || s[apos] == ')' )
cout << "(-?" << endl;
if ( s[apos] == '[' || s[apos] == ']' )
cout << "[-?" << endl;
if ( s[apos] == '{' || s[apos] == '}' )
cout << "{-?" << endl;
}
else {
if ( s[apos] == '$' )
cout << "?-*/" << endl;
else cout << "?-" << s[apos] << endl;
}
}
}
int main() {
/*ios::sync_with_stdio(false);
cin.tie(nullptr);
cin.tie(nullptr);*/
sav['$'] = '@';
sav[')'] = '(';
sav['}'] = '{';
sav[']'] = '[';
sav['@'] = '$';
sav['('] = ')';
sav['['] = ']';
sav['{'] = '}';
char s1[10000];
scanf("%[^EOF]", s1);
s = move(s1);
del();
solved();
return 0;
}