Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

o948's blog

By o948, history, 8 years ago, In English

Create a new link on your bookmark bar with the following URL:

javascript:for(var data="",inputs=document.querySelectorAll(".input pre"),i=0;i<inputs.length;i++){var s=inputs[i].innerHTML;s=s.replace(/<br>/g,"\n"),s=s.replace(/\n*$/,"\n\n");var div=document.createElement("div");div.innerHTML=s,s=div.textContent,s=s.replace(/\n/g,"\r\n"),data+=s}var a=document.createElement("a");a.setAttribute("href","data:text/plain,"+encodeURIComponent(data)),a.setAttribute("download","tests.txt"),document.body.appendChild(a),a.click();

which is a minified version of this:

var data = "";

var inputs = document.querySelectorAll(".input pre");
for (var i = 0; i < inputs.length; i++) {
var s = inputs[i].innerHTML;

// fix spacing
s = s.replace(/<br>/g, "\n");
s = s.replace(/\n*$/, "\n\n");

// decode html entities
var div = document.createElement("div");
div.innerHTML = s;
s = div.textContent;

// for Windows
s = s.replace(/\n/g, "\r\n");

data += s;
}

var a = document.createElement("a");
a.setAttribute("href", "data:text/plain," + encodeURIComponent(data));
a.setAttribute("download", "tests.txt");

document.body.appendChild(a);
a.click();

Clicking this bookmark will download a file with all the sample tests from currently opened problem statement.

You can run your solution with all the sample inputs like this:

int main() {
#ifndef ONLINE_JUDGE
freopen("tests.txt", "r", stdin);
#endif

int n;
while (cin >> n) {
// solve
}
return 0;
}

Cheers!

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

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For every newline character, instead of adding a space or a newline, it adds nothing, which has the consequence of the last integer on one line and the first integer of the next line getting merged into one integer.

For example, for the sample input of:

3
1 1
7 5
1 5

The resulting tests.txt file ends up with:

31 17 51 5

This also occurs between sample inputs, at the very end of one, and the start of the next. This cause the input to be incorrect defeating the purpose of the script.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Does changing "\n" to "\r\n" in the script fix the problem?
    (I'm a linux user, newlines on my system are just "\n".)

    • »
      »
      »
      8 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Nope, I tried "\n", "\r\n", "\n\r" and "\r". None of them worked

      • »
        »
        »
        »
        8 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Which browser are you using? Are you under Windows? I'll try to get my hands on same environment and see what's the problem.

        • »
          »
          »
          »
          »
          8 years ago, # ^ |
          Rev. 3   Vote: I like it 0 Vote: I do not like it

          Chrome (v49.0.2623.87 to be exact) on Windows 10, thank you for your time

          • »
            »
            »
            »
            »
            »
            8 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Thanks, the problem was using "\n" instead of "\r\n". Also when you tried replacing "\n" to "\r\n" it didn't work for you because Node.textContent was eating "\r"s.

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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