网易互娱 文件系统
import java. util. *;
public class Main {
private static int count = 0 ;
private static Queue< Integer> queue = new ArrayDeque < > ( ) ;
private static Map< Integer, String> hashmap = new HashMap < > ( ) ;
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
int loop = sc. nextInt ( ) ;
while ( loop> 0 ) {
int m = sc. nextInt ( ) ;
String rubish = sc. nextLine ( ) ;
while ( m> 0 ) {
String[ ] s = sc. nextLine ( ) . split ( " " ) ;
if ( s[ 0 ] . equals ( "open" ) ) open ( s) ;
else if ( s[ 0 ] . equals ( "dup" ) ) dup ( s) ;
else if ( s[ 0 ] . equals ( "dup2" ) ) dup2 ( s) ;
else if ( s[ 0 ] . equals ( "query" ) ) query ( s) ;
else if ( s[ 0 ] . equals ( "close" ) ) close ( s) ;
m-- ;
}
hashmap = new HashMap < > ( ) ;
count = 0 ;
loop-- ;
}
}
public static void open ( String[ ] s) {
if ( ! queue. isEmpty ( ) ) {
int index = queue. poll ( ) ;
System. out. println ( index) ;
hashmap. put ( index, s[ 1 ] ) ;
} else {
System. out. println ( "???" + count) ;
hashmap. put ( count, s[ 1 ] ) ;
count++ ;
}
}
public static void dup ( String[ ] s) {
String file = hashmap. get ( Integer. valueOf ( s[ 1 ] ) ) ;
if ( ! queue. isEmpty ( ) ) {
int index = queue. poll ( ) ;
hashmap. put ( index, file) ;
System. out. println ( index) ;
} else {
hashmap. put ( count, file) ;
System. out. println ( count) ;
count++ ;
}
}
public static void dup2 ( String[ ] s) {
int fd = Integer. valueOf ( s[ 1 ] ) ;
int newfd = Integer. valueOf ( s[ 2 ] ) ;
String file = hashmap. get ( fd) ;
if ( file!= null) hashmap. put ( newfd, file) ;
}
public static void close ( String[ ] s) {
int index = Integer. valueOf ( s[ 1 ] ) ;
queue. add ( index) ;
hashmap. remove ( index) ;
}
public static void query ( String[ ] s) {
int index = Integer. valueOf ( s[ 1 ] ) ;
String file = hashmap. get ( index) ;
System. out. println ( file) ;
}
}
样例
2
10
open libc.so
open libm.so
open libdl.so
dup 2
dup2 0 2
close 0
query 1
query 2
query 3
open log.txt
14
open output.txt
dup2 0 1000000
query 0
query 1000000
close 0
open output2.txt
dup2 0 100000
query 0
query 1000000
close 0
open 1.txt
dup 100000
query 1
query 0
# 输出
0
1
2
3
libm.so
libc.so
libdl.so
0
0
output.txt
output.txt
0
output2.txt
output.txt
0
1
output2.txt
1.txt