282 - Rename

All about problems in Volume 2. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

282 - Rename

Post by Adrian Kuegel »

I think the problem in its form is now simplified compared to the original problem.
Think of following case:
Files are:
a.c
ab.c

command:
rename a*.c ab*.c

the output we have to produce according to the changed problem description is:
mv a.c ab.c
mv ab.c abb.c

But clearly, doing this in unix would overwrite file ab.c. So I guess the original problem wanted cases like that to be handled properly like:
mv ab.c abb.c
mv a.c ab.c

However, this change is probably not as important, handling cases like the one I mentioned correctly is also rather easy, of course it would be tricky to see that this can happen.
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

Hmm. Good point, but I doubt that the intention of the original problem was to handle these cases. If so, how should we handle:
Files:
a.c
b.c
Command:
rename a.* b.*

It becomes inavoidable to have overwrites, so I think the best approach is the 'naive' approach and let things happen as they happen. And then any execution order is as good as any other one. I added the line to the description to avoid writing a corrector (as you probably guessed).

BTW: Is the behaviour of the mv command specified in case an existing file gets overwritten? My Linux prompts for confirmation. MS-Dos (at least the XP-prompt) will give an error message.
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

I just tried my example with ms-dos and it gives an error message. It seems the rename command first checks if some filename would be changed to a already existing filename before actually starting the renaming. So you are probably right that this was not the intention of the problem.
Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

Is trivial/greedy enough? Or am I missing a trick case?

For:

Code: Select all

abFile001.c
abFile001.cxx
abprog001.c
abfile.c
abFile.c
abFileprog.c
end
rename abFile*.c bprog*.cxx
rename a*.c *
end
acm.c
end
rename ac*.c ib*.cpp
end
a.c
ab.c
end
rename a*.c ab*.c
end
a.c
b.c
end
rename a.* b.*
rename *.c *.d
end
I get:

Code: Select all

rename abFile*.c bprog*.cxx
mv abFile001.c bprog001.cxx
mv abFile.c bprog.cxx
mv abFileprog.c bprogprog.cxx
rename a*.c *
mv abFile001.c bFile001
mv abprog001.c bprog001
mv abfile.c bfile
mv abFile.c bFile
mv abFileprog.c bFileprog

rename ac*.c ib*.cpp
mv acm.c ibm.cpp

rename a*.c ab*.c
mv a.c ab.c
mv ab.c abb.c

rename a.* b.*
mv a.c b.c
rename *.c *.d
mv a.c a.d
mv b.c b.d

Can someone check please? Thanks.
stubbscroll
Experienced poster
Posts: 151
Joined: Tue Nov 16, 2004 7:23 pm
Location: Norway
Contact:

Post by stubbscroll »

My AC program produces the same output. Try this input:

Code: Select all

ab
abab
end
rename ab*ab asd*asd
end
output:

Code: Select all

rename ab*ab asd*asd
mv abab asdasd
Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

That's a good case, I totally didn't realize! Thanks! :D
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

Nice IO, but if I correct remeber, MS-DOS cannot evaluate comands like:

rename a*a b*b

(asteriks in the middle of file is not allowed). Asteriks means (in MS-DOS) all characters from this point to the dot or end of file name (if dot in name doesn't exist). So why we should think about this problem ? Problem statement don't say any word about such cases :) so I guess author (of IO setter) think, that such cases are tricky ;) but in my opinion they are incorrect. Little joey, are you agree with me ?

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

Well, the problem statement states that both operands contain exactly one wildcard character but nothing is further said about it. This implies that all four forms (*, x*, *y and x*y) are possible in any combination. It is further explicitly stated (in the note) that we are not emulating the behaviour of MS-DOS, but letting the wildcard character match any number of printable characters (with the 14 character filename restriction). Constructions like "rename a*b b*a" and "rename * x*y" are perfectly legal according to the text.

So, no Dominik, I don't think these cases are tricky or incorrect.

Happy hunting!
-little joey

PS. It would be cruel to require knowledge of MS-DOS to solve a programming problem, especially because there are thirty-something versions and sub-versions of the thing, all with their own bugs/features. It is my strong conviction that people tend to live longer and happier without any knowledge of MS-DOS. I, alas, know too much about it...
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

Unfortunatly I know MS-DOS from 3.30 version til 7.0 included in windows ;)

I know that any place for * is allowed by prolem statement :) but I think, that telling about MS-DOS in description is confused ;) and it's easy to make mistake :)

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

Hello ....

Hmm I doubt, that I miss something in description.

What should be output for this:

Code: Select all

a.c
b.d
end
rename a* x*
rename *d *txt
end
I use following algorithm:
0. convert input mask into DEF (as described below)
1. convert input word (using mask) to ABC, where A = symbols upto *, B = * , C = symbols after *. Of course some of this parts could be empty.
2. Compare A=D and C = F.
3. if comparing is OK, create output and print it.

I try to use also algorithm, which divide first word and mask in two part - before and after *, but I got WA too.

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
himanshu
New poster
Posts: 17
Joined: Mon May 15, 2006 12:24 pm
Location: Hyderabad, India
Contact:

Post by himanshu »

Hello,
For the following input:-

Code: Select all

a.c
ab.c
end
rename a*.c ab*.c
end
b.d
a.c
end
rename *d *txt
rename a* x*
end
abFile001.c
abFile001.cxx
abprog001.c
abfile.c
abFile.c
abFileprog.c
end
rename abFile*.c bprog*.cxx
end
acm.c
end
rename ac*.c ib*.cpp
end
ab
abab
end
rename ab*ab asd*asd
end
my program outputs:-

Code: Select all

rename a*.c ab*.c
mv a.c ab.c
mv ab.c abb.c

rename *d *txt
mv b.d b.txt

rename a* x*
mv a.c x.c

rename abFile*.c bprog*.cxx
mv abFile001.c bprog001.cxx
mv abFile.c bprog.cxx
mv abFileprog.c bprogprog.cxx

rename ac*.c ib*.cpp
mv acm.c ibm.cpp

rename ab*ab asd*asd
mv abab asdasd

and I get WA.

Could you help please with a few more test cases.

Thank You,
Himanshu.
minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity »

I pass all the test cases posted but I'm still getting WA from the Judge... Are spaces allowed as a part of a word? Can anybody see what's wrong with my code?

Code: Select all

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s, ss;

int main(){

  while(cin >> s){
    vector < string > data;
    while(s != "end"){
      data.push_back(s);
      cin >> s;
    }

    while(cin >> s && s == "rename" && cin >> s >> ss){
      cout << "rename " << s << " " << ss << endl;
      int p = s.find('*'), pp = ss.find('*');
      string s1b = s.substr(0, p), s1e = s.substr(p + 1);
      string s2b = ss.substr(0, pp), s2e = ss.substr(pp + 1);
      int sz = s1b.size() + s1e.size();

      for(int i = 0; i < (int)data.size(); i++)
	if((int)data[i].size() >= sz && (int)data[i].find(s1b) == 0)
	  if(data[i].find(s1e, data[i].size() - s1e.size()) != string::npos)
	    cout << "mv " << data[i] << " " << s2b << 
	      data[i].substr(s1b.size(), data[i].size() - sz) << s2e << endl;
    }
    cout << endl;
    
  }

  return 0;
}
Ahmad93
New poster
Posts: 6
Joined: Wed Mar 05, 2014 6:39 pm

Re:

Post by Ahmad93 »

stubbscroll wrote:My AC program produces the same output. Try this input:

Code: Select all

ab
abab
end
rename ab*ab asd*asd
end
output:

Code: Select all

rename ab*ab asd*asd
mv abab asdasd
Can anybody explain the output further? What will be the output of this command: rename ab*cdef ghi*jkl ?
And will I have to check blank spaces in the command?
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 282 Rename

Post by brianfry713 »

I don't understand your questions. The I/O posted by stubbscroll is correct. If you are asking what is the correct output of this input:

Code: Select all

ab
abab
end
rename ab*cdef ghi*jkl
end
It is:

Code: Select all

rename ab*cdef ghi*jkl

None of the files match the wildfrom.

Each command appears on one line in the form:
rename wildfrom wildto
from and to will both contain one wild-card character.

I read them as strings separated by whitespace.
Check input and AC output for thousands of problems on uDebug!
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 282 Rename

Post by lighted »

What will be output of this

Code: Select all

aFileb.c
aFile.b
c.axb
c.ab
ab.c
ab
end
rename
a*b b*a
end
baFile.c
bFilea.a
b.xa
b.a
a.c
a
ca
end
rename *a b*c
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Post Reply

Return to “Volume 2 (200-299)”