fork download
  1. library(magrittr)
  2. require(dplyr)
  3. require(data.table)
  4.  
  5. p.combination = character(10000)
  6. for(i in 1:length(p.combination)){
  7. p.combination[i] = LETTERS[sample(1:26,5)] %>% paste0(collapse = ", ")
  8. }
  9.  
  10. p.com.allowed = character(10000)
  11. for(i in 1:length(p.com.allowed)){
  12. p.com.allowed[i] = LETTERS[sample(1:26,5)] %>% paste0(collapse = ", ")
  13. }
  14.  
  15. data = data.frame(p.combination = p.combination,
  16. p.com.allowed = p.com.allowed)
  17. player = LETTERS[1:26]
  18.  
  19. #######################
  20.  
  21. t1=Sys.time()
  22.  
  23. w2long=function(array){
  24. lapply(c(1:length(array)),function(i) {
  25. data.table("id"=i,"player"=as.character(array[i]) %>%
  26. strsplit(", ") %>% .[[1]])
  27. }) %>% rbindlist()
  28. }
  29.  
  30. out=merge(
  31. w2long(data$p.combination) %>% mutate(show=1) %>%
  32. dcast(id~player,value.var="show"),
  33. w2long(data$p.com.allowed) %>% mutate(show=-1) %>%
  34. dcast(id~player,value.var="show"),by="id",suffix=c("_O","_D")
  35. )
  36. out[is.na(out)]=0
  37.  
  38.  
  39. t2=Sys.time()
  40. print(t2-t1) #在我的電腦上是7.42sec
  41. ###########################
  42.  
  43. t3=Sys.time()
  44.  
  45. input.matrix0 = function(data, player, off){
  46. X = matrix(ncol = length(player), nrow = dim(data)[1])
  47. for(i in 1:dim(data)[1]){
  48. if(off) {
  49. colnames(X) = paste0("O_",player)
  50. coding = 1
  51. pp = data$p.combination
  52. } else {
  53. colnames(X) = paste0("D_",player)
  54. coding = -1
  55. pp = data$p.com.allowed
  56. }
  57. player.temp = pp[i] %>% gsub(", ", "|",.)
  58. index = grep(player.temp, player)
  59. X[i,index] = coding
  60. X[i,-index] = 0
  61. }
  62. return(X)
  63. }
  64.  
  65. input.matrix = function(data, player){
  66. X.off = input.matrix0(data, player, T)
  67. X.def = input.matrix0(data, player, F)
  68. return(cbind(X.off, X.def))
  69. }
  70.  
  71. out = input.matrix(data,player)
  72.  
  73. t4=Sys.time()
  74. print(t4-t3) #在我的電腦上是30.67sec# your code goes here
Time limit exceeded #stdin #stdout #stderr 5s 161156KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Loading required package: dplyr

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

Loading required package: data.table
------------------------------------------------------------------------------
data.table + dplyr code now lives in dtplyr.
Please library(dtplyr)!
------------------------------------------------------------------------------

Attaching package: ‘data.table’

The following objects are masked from ‘package:dplyr’:

    between, first, last

/bin/bash: line 15:  2925 CPU time limit exceeded R --vanilla --quiet --slave --encoding=UTF-8 --file=/home/6NymLE/prog.r > a.out