Ruby:如何读取跨越几行的字符串?
背景: 在编程竞赛中,测试用例通常非常大。当问题涉及字符串时,这些给定字符串可能由10^5个或更多字符组成 请参阅此输入: 以下是我试图解决的问题: 下面是我的RUBY解决方案,它只通过了10个给定测试用例中的3个:Ruby:如何读取跨越几行的字符串?,ruby,gets,Ruby,Gets,背景: 在编程竞赛中,测试用例通常非常大。当问题涉及字符串时,这些给定字符串可能由10^5个或更多字符组成 请参阅此输入: 以下是我试图解决的问题: 下面是我的RUBY解决方案,它只通过了10个给定测试用例中的3个: swapflag = 0 n = gets.to_i for i in 0..n-1 tc = gets tc2 = gets if tc.length > tc2.length tmp = tc2 tc2 = tc
swapflag = 0
n = gets.to_i
for i in 0..n-1
tc = gets
tc2 = gets
if tc.length > tc2.length
tmp = tc2
tc2 = tc
tc = tmp
swapflag = 1
end
tc.delete! " "
tc2.delete! " "
tc.split("").each do |x|
if tc2.include? x
#print x
tc = tc.sub(x,'')
tc2 = tc2.sub(x,'')
end
end
#puts tc, tc2
if tc.length > 0 and tc2.length == 0 and swapflag == 0
puts "You win some."
elsif tc.length > 0 and tc2.length == 0 and swapflag == 1
puts "You lose some."
elsif tc.length == 0 and tc2.length > 0 and swapflag == 0
puts "You lose some."
elsif tc.length == 0 and tc2.length > 0 and swapflag == 1
puts "You win some."
else
puts "You draw some."
end
swapflag = 0
end
经过检查,我用来读取输入行的get函数似乎在字符串变大并跨越多行时无法读取字符串
有没有办法告诉ruby读取跨越几行的字符串
提前谢谢
我还不能发布图像,少于10的声誉可以读取最大长度的输入行(在那里!)。据我所知,您可以使用
get
读取的字符数仅受代码可用内存量的限制,并且它们提供了足够的内存
在链接的输入中,第一行后面实际上有10*2=15行。只是在浏览器中,长的行被包装起来,看起来像是被分割成多行。因此,使用两个get
就足以读取一个测试用例的两行
所以你读的行很好,你必须检查读过的部分