282 - Rename
Moderator: Board moderators
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
282 - Rename
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.
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.
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
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.
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.
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
-
- Guru
- Posts: 647
- Joined: Wed Jun 26, 2002 10:12 pm
- Location: Hong Kong and New York City
- Contact:
Is trivial/greedy enough? Or am I missing a trick case?
For:
I get:
Can someone check please? Thanks.
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
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
-
- Experienced poster
- Posts: 151
- Joined: Tue Nov 16, 2004 7:23 pm
- Location: Norway
- Contact:
My AC program produces the same output. Try this input:
output:
Code: Select all
ab
abab
end
rename ab*ab asd*asd
end
Code: Select all
rename ab*ab asd*asd
mv abab asdasd
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
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
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
![:)](./images/smilies/icon_smile.gif)
![;)](./images/smilies/icon_wink.gif)
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)
Born from ashes - restarting counter of problems (800+ solved problems)
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
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...
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...
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
Unfortunatly I know MS-DOS from 3.30 version til 7.0 included in windows ![;)](./images/smilies/icon_wink.gif)
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 ![:)](./images/smilies/icon_smile.gif)
Best regards
DM
![;)](./images/smilies/icon_wink.gif)
I know that any place for * is allowed by prolem statement
![:)](./images/smilies/icon_smile.gif)
![;)](./images/smilies/icon_wink.gif)
![:)](./images/smilies/icon_smile.gif)
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)
Born from ashes - restarting counter of problems (800+ solved problems)
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
Hello ....
Hmm I doubt, that I miss something in description.
What should be output for this:
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
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
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)
Born from ashes - restarting counter of problems (800+ solved problems)
Hello,
For the following input:-
my program outputs:-
and I get WA.
Could you help please with a few more test cases.
Thank You,
Himanshu.
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
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
Could you help please with a few more test cases.
Thank You,
Himanshu.
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;
}
Re:
Can anybody explain the output further? What will be the output of this command: rename ab*cdef ghi*jkl ?stubbscroll wrote:My AC program produces the same output. Try this input:
output:Code: Select all
ab abab end rename ab*ab asd*asd end
Code: Select all
rename ab*ab asd*asd mv abab asdasd
And will I have to check blank spaces in the command?
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 282 Rename
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:It is: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.
Code: Select all
ab
abab
end
rename ab*cdef ghi*jkl
end
Code: Select all
rename ab*cdef ghi*jkl
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!
Re: 282 Rename
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