|
|
back to boardTLE(c++)? Posted by mah1991 19 Nov 2010 16:40 why do i get Time limit exceeded :( #include <cstring> #include <cstdlib> #include <cstdio> using namespace std; int main () { char * pch; char str[30005],str1[30005]; int number,i=0,j=0; scanf("%d",&number); while(i<number) { scanf("%s",str); i+=strlen(str); strcat(str1,str); } while(strstr(str1,"><")) { pch=strstr(str1,"><"); strncpy(pch,"<>",2); j++; } printf("%d",j); return 0; } Re: TLE(c++)? I used very similar code and got AC (though there is a better solution if I recall correctly). What I did is: - If you have <<< at left end or >>> at right and, you can ignore those - they will never change again. Thus, you can keep treack of the left and right endings, and only parse those, instead of going from 0 to n every time. AC 0.625 Re: TLE(c++)? Posted by Pegasus 23 Nov 2012 09:29 #include <iostream> using namespace std; int main() { int n; cin >> n; char c; int t = 0, ans = 0; while (n--) { cin >> c; if (c == '>') ++t; else ans += t; } cout << ans << endl; return 0; } |
|
|