object_oriented_program's blog

By object_oriented_program, 9 years ago, In English

In the recent codechef contest, I solved the sum DIVGOLD using lists in Python.

Here is the code

for i in xrange(0,n):
		foo=list(s)
		k=foo.pop(i)
		for j in xrange(0,n):
			foo.insert(j,k)
			minm=''.join(foo)
			ans=min(minm,ans)
			foo.pop(j)

I am unable to implement it using list in STL. Erasing the element and inserting elements doesnt seem to work. Any solutions ?

Edit: Seems that there was a fault in my looping logic. Implementation using strings seems to be a much better option.


string s,ans,res; cin >> n; cin >> s; ans = s; for(i = 0; i < n; i++) { ch = s[i]; s.erase(s.begin()+i,s.begin()+i+1); for(j = 0; j < n; j++) { s.insert(j,1,ch); ans = min(ans,s); s.erase(s.begin()+j,s.begin()+j+1); } s.insert(i,1,ch); } cout << ans << endl;
| Write comment?
»
9 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Use string instead of list