我目前正在使用Codeforces problem(我不是在比赛的中间,所以它不会作弊!)我无法获得字符串比较部分由于某种原因正确。
问题的关键在于,给定一个字符串数组,我必须计算我必须删除多少列才能按字典顺序排列字符串。因此,对于第二个测试用例场景,我们已经给出了
case
care
test
code
答案应该是2,第一列和第三列被删除并导致
ae
ae
et
oe
以下是我的代码:
#some code to take the input and convert it to a list s
begin = 0
out_list = [[] for i in range(n)]
while begin < m: #m is the length of each string
out_copy = out_list[:]
for i in range(n): #n is the number of strings
out_copy[i].append(s[i][begin])
fine = True
for i in range(1,n):
if ''.join(out_copy[i]) < ''.join(out_copy[i-1]):
fine = False
if fine:
out_list = out_copy
begin += 1
#some code to output the result
''.join(list)
应该给我一个字符串,它是列表中所有字符的串联,而<
运算符应该足以让计算机返回{{1}因为False
小于c
,所以第一列...但是这里的某些内容一定不是正确的。发生了什么事?
答案 0 :(得分:1)
使用此行
out_copy = out_list[:]
你制作了out_list
out_list[i]
。因此,out_copy[i]
和out_copy[i]
指的是同一个对象。
请注意,您在每次迭代时都会变异out_list[i]
,因此无论fine
值如何,每次迭代都会更改print out_list
。您可以通过在循环中添加begin = 0
out_list = [[] for i in range(n)]
while begin < m: #m is the length of each string
print out_list
out_copy = out_list[:]
.....
.....
来确认它
[[], [], [], []]
[['c'], ['c'], ['t'], ['c']]
[['c', 'a'], ['c', 'a'], ['t', 'e'], ['c', 'o']]
[['c', 'a', 's'], ['c', 'a', 'r'], ['t', 'e', 's'], ['c', 'o', 'd']]
输出
import copy
begin = 0
out_list = [[] for i in range(n)]
while begin < m: #m is the length of each string
out_copy = copy.deepcopy(out_list)
.....
.....
print out_list
改为制作深层照片。
[['a', 'e'], ['a', 'e'], ['e', 't'], ['o', 'e']]
产生正确的结果
require 'openssl'
begin
require 'origami'
rescue LoadError
ORIGAMIDIR = "C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\origami-1.2.4\lib"
$: << ORIGAMIDIR
require 'origami'
end
include Origami
# Code below is based on documentation available on
# http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL.html
key = OpenSSL::PKey::RSA.new 2048
open 'private_key.pem', 'w' do |io| io.write key.to_pem end
open 'public_key.pem', 'w' do |io| io.write key.public_key.to_pem end
cipher = OpenSSL::Cipher::Cipher.new 'AES-128-CBC'
pass_phrase = 'Origami rocks'
key_secure = key.export cipher, pass_phrase
open 'private_key.pem', 'w' do |io|
io.write key_secure
end
#Create the certificate
name = OpenSSL::X509::Name.parse 'CN=nobody/DC=example'
cert = OpenSSL::X509::Certificate.new
cert.version = 2
cert.serial = 0
cert.not_before = Time.now
cert.not_after = Time.now + 3600
cert.public_key = key.public_key
cert.subject = name
OUTPUTFILE = "test.pdf"
contents = ContentStream.new.setFilter(:FlateDecode)
contents.write OUTPUTFILE,
:x => 350, :y => 750, :rendering => Text::Rendering::STROKE, :size => 30
pdf = PDF.read('Sample.pdf')
# Open certificate files
#sigannot = Annotation::Widget::Signature.new
#sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
#page.add_annot(sigannot)
# Sign the PDF with the specified keys
pdf.sign(cert, key,
:method => 'adbe.pkcs7.sha1',
#:annotation => sigannot,
:location => "Portugal",
:contact => "myemail@email.tt",
:reason => "Proof of Concept"
)
# Save the resulting file
pdf.save(OUTPUTFILE)