简体   繁体   English

如何从 Codeforces 获取多行输入?

[英]How do I take multi-line input from Codeforces?

Codefores requires a lot of multi-line input. Codefores 需要大量的多行输入。 For example:例如:

https://codeforces.com/contest/71/problem/A https://codeforces.com/contest/71/problem/A

TLDR: read this and reduce length of all words: TLDR:阅读并减少所有单词的长度:

4
word
localization
internationalization
pneumonoultramicroscopicsilicovolcanoconiosis

I used this solution, which I believe is correct and works for me:我使用了这个解决方案,我认为它是正确的并且对我有用:

lines = []
while True:
    line = input()
    if line:
        lines.append(line)
    else:
        break
input = '\n'.join(lines)


tab=input.splitlines()

numb=tab[0]

tab.pop(0)

for i in tab:
    wordTab=[]
    if len(i)>10:
        wordTab.append(i[:1])
        wordTab.append(i[-1:])
        print(f"{i[:1]}{len(i)-2}{i[-1:]}")
    else:
        print(i)

Yet i got an error (on their side).然而我得到了一个错误(在他们这边)。 How can I make Codeforces accept multi-line input in Python?如何让 Codeforces 接受 Python 中的多行输入?

I have a setup where I have two files called "input.txt" and "output.txt".我有一个设置,其中有两个名为“input.txt”和“output.txt”的文件。 That is where I write the code locally and then I have a FILE params which I make it True and False when I have to submit to Codeforces.那是我在本地编写代码的地方,然后我有一个 FILE 参数,当我必须提交给 Codeforces 时,我将其设为 True 和 False。 Here is an example of the same problem you had mentioned.这是您提到的相同问题的示例。 It worked fine with 77 ms as running time.它运行良好,运行时间为 77 毫秒。

import sys
from os import path

FILE = False # if needed change it while submitting

if FILE:
    sys.stdin = open('input.txt', 'r')
    sys.stdout = open('output.txt', 'w')

def get_int():
    return int(sys.stdin.readline())

def get_string():
    return sys.stdin.readline().strip()

n = get_int()

final_result = []
for i in range(n):
    word = get_string()
    if len(word) > 10:
        word = word[0] + str(len(word) - 2) + word[-1]
    final_result.append(word)

for item in final_result:
    sys.stdout.write(item)
    sys.stdout.write('\n')

Here is my answer这是我的答案

n = int(input())
arr = []
 
for i in range(n) :
    word = input()
    if len(word) > 10 :
        arr.append(word[0] + str(len(word) - 2) + word[len(word)-1])
    else :
        arr.append(word)
 
print("\n".join(arr))
lines = '''4 word localization internationalization pneumonoultramicroscopicsilicovolcanoconiosis''' input = '\n'.join(lines) tab=lines.splitlines() for i in tab: wordTab=[] if len(i)>10: start= i[:1] middle = len(i)-2 end= i[-1:] print(f"{start}{len(i)-2}{end}") else: print(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM