emu in blogjava

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  171 随笔 :: 103 文章 :: 1052 评论 :: 2 Trackbacks

Problem Statement

     You are given a collection of music files that have each been tagged with the artist, album, track number, and song title. However, the actual file names do not follow any standard format. Given a desired naming format, determine the proper names for all of the files in the collection. You are given four String[]s, artists, albums, tracks, and titles, which contain the artist, album, track number, and song title, respectively, for all the music files. Element i of each String[] corresponds to the ith file. You are given the desired file name format as a String, format. This String will contain only characters from "ABTS -()._" (quotes for clarity only). To determine a file name, replace all occurrences of 'A', 'B', 'T', and 'S' in format with the artist, album, track number, and song title, respectively. All other characters should remain untouched. For example, if format = "A - B - T) S", the artist is "The Beatles", the album is "Rubber Soul", the track number is "07", and the song title is "Michelle", the file should be named "The Beatles - Rubber Soul - 07) Michelle". Return a String[] containing the file names for all the songs in the collection. The ith element of the return String[] should correspond to the song represented by the ith elements of the four String[] parameters.

Definition

    
Class: SongRenamer
Method: rename
Parameters: String[], String[], String[], String[], String
Returns: String[]
Method signature: String[] rename(String[] artists, String[] albums, String[] tracks, String[] titles, String format)
(be sure your method is public)
    

Constraints

- artists, albums, tracks, and titles will each contain between 1 and 20 elements, inclusive.
- artists, albums, tracks, and titles will each contain the same number of elements.
- Each element of artists, albums, tracks, and titles will contain between 1 and 20 characters, inclusive.
- Each element of artists, albums, tracks, and titles will contain only letters ('a'-'z', 'A'-'Z'), digits ('0'-'9'), and spaces.
- format will contain between 1 and 10 characters, inclusive.
- format will contain only characters from the String "ABTS -()._" (quotes for clarity only).

Examples

0)
    
{"Marvin Gaye", "Marvin Gaye"}
{"Here My Dear", "Whats Going On"}
{"09", "7"}
{"Annas Song", "Right On"}
"A - B-T-S"
Returns: 
{"Marvin Gaye - Here My Dear-09-Annas Song",
 "Marvin Gaye - Whats Going On-7-Right On" }
1)
    
{"Booker T and the MGs"}
{"McLemore Avenue"}
{"Number Two"}
{"Something"}
"T. S_B_(S)"
Returns: {"Number Two. Something_McLemore Avenue_(Something)" }
format can contain multiple instances of the same tag. Also, track numbers do not have to contain only digits.
2)
    
{"The Beatles", "The Supremes", "YellowMatterCustard"}
{"A Hard Days Night", "A Bit Of Liverpool", "One Night In NYC"}
{"Twelve", "Siete", "7"}
{"You Cant Do That", "You Cant Do That", "You Cant Do That"}
"S (S) S"
Returns: 
{"You Cant Do That (You Cant Do That) You Cant Do That",
 "You Cant Do That (You Cant Do That) You Cant Do That",
 "You Cant Do That (You Cant Do That) You Cant Do That" }
3)
    
{"  The Leading Spaces"}
{"  "}
{"Trailing Space  "}
{" s p a  c e s "}
"S._A_B(T)"
Returns: {" s p a  c e s ._  The Leading Spaces_  (Trailing Space  )" }
Preserve all spaces.
4)
    
{"Ignored"}
{"Unnoticed"}
{"000"}
{"Uncredited"}
"()-(). "
Returns: {"()-(). " }
format doesn't necessarily have to contain any tags.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

posted on 2005-08-25 11:08 emu 阅读(1493) 评论(3)  编辑  收藏 所属分类: google编程大赛模拟题及入围赛真题

评论

# re: SongRenamer (入围赛250分真题) 2005-09-29 19:13 huangyi
说说我的想法 .net的

先使用string.replace,转换format
a->{0}, b->{1}, t->{2}, s->{3}

然后就可以直接 filenames[i] = string.Format(formatString,artists[i], albums[i], tracks[i], ,titles[i]);

  回复  更多评论
  

# re: SongRenamer (入围赛250分真题) 2005-12-11 22:35 小飞侠
public class test {
public static String[] rename(String[] artists, String[] albums, String[] tracks, String[] titles, String format) {
StringBuffer[] ret;
String[] rets;
int i, n, j;

n = format.length();
ret = new StringBuffer[artists.length];
//初始化
for (i = 0; i < ret.length; i++)
ret[i] = new StringBuffer();

for (j = 0; j < artists.length; j++) {
for (i = 0; i < n; i++) {
switch(format.charAt(i)) {
case 'A' :
ret[j].append(artists[j]);
break;
case 'B' :
ret[j].append(albums[j]);
break;
case 'T' :
ret[j].append(tracks[j]);
break;
case 'S' :
ret[j].append(titles[j]);
break;
default :
ret[j].append(format.charAt(i));
break;
}
}
}

rets = new String[ret.length];
for (i = 0; i < ret.length; i++) {
rets[i] = ret[i].toString();
}

for (i = 0; i < rets.length; i++) {
System.out.println(rets[i]);
}

return rets;
}

public static void main(String args[]) {
String[] artists = {"Ignored"},
albums = {"Unnoticed"},
tracks = {"000"},
titles = {"Uncredited"};
String format = "()-(). ";

rename(artists, albums, tracks, titles, format);
}
}  回复  更多评论
  

# emu的解法 2005-12-12 12:09 emu
小飞侠,应该吧打印结果部分从函数中抽出来。
我的解法是:

public class SongRenamer
{
public String[] rename(String[] artists, String[] albums, String[] tracks, String[] titles, String format){
String[] result = new String[artists.length];
for(int i=0;i<result.length;i++){
result[i] = format
.replaceAll("A","\0\1")
.replaceAll("B","\0\2")
.replaceAll("T","\0\3")
.replaceAll("S","\0\4")
.replaceAll("\0\1",artists[i])
.replaceAll("\0\2",albums[i])
.replaceAll("\0\3",tracks[i])
.replaceAll("\0\4",titles[i]);
}
return result;

}
public static void main(String[] args){
SongRenamer s = new SongRenamer();
String[] result = s.rename(
new String[]{"Marvin Gaye", "Marvin Gaye"},
new String[]{"Here My Dear", "Whats Going On"},
new String[]{"09", "7"},
new String[]{"Annas Song", "Right On"},
"A - B-T-S");
System.out.println(java.util.Arrays.asList(result));

result = s.rename(
new String[]{"The Beatles", "The Supremes", "YellowMatterCustard"},
new String[]{"A Hard Days Night", "A Bit Of Liverpool", "One Night In NYC"},
new String[]{"Twelve", "Siete", "7"},
new String[]{"You Cant Do That", "You Cant Do That", "You Cant Do That"},
"S (S) S");
System.out.println(java.util.Arrays.asList(result));

result = s.rename(
new String[]{" The Leading Spaces"},
new String[]{" "},
new String[]{"Trailing Space "},
new String[]{" s p a c e s "},
"S._A_B(T");
System.out.println(java.util.Arrays.asList(result));

result = s.rename(
new String[] {"Ignored"},
new String[]{"Unnoticed"},
new String[]{"000"},
new String[]{"Uncredited"},
"()-(). ");
System.out.println(java.util.Arrays.asList(result));
}
}
  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: