Submission #175851

#TimeUsernameProblemLanguageResultExecution timeMemory
175851Ruxandra985Match (CEOI16_match)C++14
37 / 100
2050 ms15368 KiB
#include <bits/stdc++.h> #pragma GCC optimize "Ofast" using namespace std; int sp[30][100010]; char sol[100010] , s[100010]; int ok; int stk[100010] , pre[30][100010]; int match (int st , int dr){ int i , elem = 0; if (st > dr) return 1; for (i=0;i<'z' - 'a';++i){ if (((sp[i][dr] - sp[i][st-1]) & 1) == 1) return 0; } for (i=st;i<=dr;++i){ if (stk[elem] == s[i]) elem--; else stk[++elem] = s[i]; } return (elem == 0); } void solve (int st , int dr){ int i; if (st > dr) return; if (!ok) /// nu mai are rost return; /// stii ca pe st pui ( , gaseste i match ul for (i = pre[s[st]][dr]; i >= st + 1 ; i = pre[s[st]][i-1]){ if (match(st + 1 , i - 1) && match(i + 1 , dr)) break; } if (i < st + 1){ ok = 0; return; } else { sol[st] = '('; sol[i] = ')'; solve(st + 1 , i - 1); solve (i + 1 , dr); } } int main() { FILE *fin = stdin; FILE *fout = stdout; int n , i; fgets (s+1 , 100010 , fin); n = 0; while ( 'a' <= s[n+1] && s[n+1] <= 'z' ){ n++; s[n]-='a'; for (i=0;i<='z'-'a';++i){ sp[i][n] = sp[i][n-1] + (s[n] == i); if (i == s[n]) pre[i][n] = n; else pre[i][n] = pre[i][n-1]; } } stk[0] = -1; ok = 1; solve (1 , n); if (ok == 0){ fprintf (fout,"-1"); } else { fputs (sol+1,fout); } return 0; }

Compilation message (stderr)

match.cpp: In function 'void solve(int, int)':
match.cpp:33:23: warning: array subscript has type 'char' [-Wchar-subscripts]
     for (i = pre[s[st]][dr]; i >= st + 1 ; i = pre[s[st]][i-1]){
                       ^
match.cpp:33:57: warning: array subscript has type 'char' [-Wchar-subscripts]
     for (i = pre[s[st]][dr]; i >= st + 1 ; i = pre[s[st]][i-1]){
                                                         ^
match.cpp: In function 'int main()':
match.cpp:53:11: warning: ignoring return value of 'char* fgets(char*, int, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     fgets (s+1 , 100010 , fin);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:936:0,
                 from /usr/include/c++/7/cstdio:42,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:46,
                 from match.cpp:1:
In function 'char* fgets(char*, int, FILE*)',
    inlined from 'int main()' at match.cpp:53:11:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:261:58: warning: call to '__fgets_chk_warn' declared with attribute warning: fgets called with bigger size than length of destination buffer
  return __fgets_chk_warn (__s, __bos (__s), __n, __stream);
                                                          ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...