SphericalCow's blog

By SphericalCow, history, 7 weeks ago, In English

Personally, I like to format my code as cleanly and simplistic as possible without much of templates on top of it. On the other hand, having templates can give the advantage in terms of shortening and optimizing code alongside saving time during contests. I was wondering if it were possible to add functionality to codeforces (and other websites) that allows you to submit the main and the template program (as a header file) separately into the same problem submission for the sake of cleanliness. So instead of putting 200-300 lines of templates above, the main program could just communicate with the template in a separate file through #include "template.h".

Also since you would have the same template for a long time I was thinking maybe we can link this template file as a "default template" into your profile so whenever #include "template.h" is called, codeforces would use the last template saved if the user wants to use this functionality. In addition to that, whenever some users wanted to see other people code, they wouldnt need to scroll far down passing all the templates. Probably would be useful in irl contest if the committee allows template.

Out of curiousity, is this a possible or even reasonable feature that can be implemented or are there reasons to not include this into cp? Maybe not possible for security reasons? Thank you.

  • Vote: I like it
  • +38
  • Vote: I do not like it

»
7 weeks ago, # |
  Vote: I like it +6 Vote: I do not like it

Indeed, it's a good idea, but you can make your own snippets if you don't want a long template. Whenever you want a part of code, for example, a segment tree template, you can call it with a snippet. It will do for now.

»
7 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

AtCoder has done something similarly with their AtCoder Library set. It is a library which implements plenty of data structures and algorithms that can be used by any user submitting on AtCoder by simply add #include "atcoder/all". Codeforces could do something similarly to this, it would definitely be interesting but I don't see it coming anytime soon.

Even then I don't think submitting your own template file would be a thing. Submitting multiple files is a hard thing to handle. Will there be 2 separate submitting buttons for 2 different files or will you select files at once? What happens to the code pastebox then? What if you want to submit not only 2 but multiple files? What if someone abuses it to bypass the 2KB file size limit to snuck in a lookup table for a particular problem, or even worse for bad intentions?

  • »
    »
    7 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    What could be the potential bad intention and security reason?

»
7 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by SphericalCow (previous revision, new revision, compare).

»
7 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

If you just want the part above your main function to be clean, there is an alternative solution which works for C++: you can write your templates below the main function and use #include __FILE__ together with an include guard.

For example:

#ifndef uwu
#define uwu
#include __FILE__
  // write your main function here
  // int main() {}
#else
  // put your templates here
  // #include <bits/stdc++.h>
#endif

Since (I guess) Codeforces gives "Judgement Failed" if your code contains some sort of #include __FILE__, you can add some empty string to the left of __FILE__ (ex. #include uwu __FILE__).

262571520

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Extending from your idea, if we can upload an additional file template.cpp, CF can generate a dynamically-linked library which only needs to be compiled one time and can be reused in different problems. This will reduce judging time in contests.

»
5 weeks ago, # |
  Vote: I like it -7 Vote: I do not like it

a suggestion: in cf there be a place to import your template once and for all, then every time just put: #include <bgopc/template.cpp> for example

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Good idea