0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

今日の問題

↑押してください。

自分の回答(javascript)

function Main(input){
    min = 1e18
    max = -1e18
    let abc = "abcdefghijklmnopqrstuvwxyz".split("")
    let ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")
    let f = 2
    if(f == 0){
      input = parseInt(input.trim())
    }
    if(f == 1){
      input = input.trim().split("\n").map((a)=>parseInt(a))
    }
    if(f == 2){
      input = input.trim().split("\n").map((a)=>a.split(" ").map((b)=>parseInt(b)))
    }
    //ここより上は定型文です。
   n = input.shift()[0]
   alist = input.shift()
   mae = -1
   ok = false
   list = []
   res = 0
   set = new Set()
   for(let i = 0;i<n;i++){
     //console.log(set)
     if(ok){
       if(!set.has(alist[i])&&alist[i]==alist[i+1]){
         count += 2
         i ++
         set.add(alist[i])
         res = Math.max(res,count)
       }else{
         if(set.has(alist[i])){
           if(alist[i] == alist[i-1]){
             count = 2
             start = i-1
             set = new Set()
             set.add(alist[i])
             continue
           }else{
             while(true){
               
               if(alist[start] == alist[i]){
                 start += 2
                 i++
                 break
               }
               set.delete(alist[start])
               start += 2
               count -= 2
             }
           }
         }else{
           mae = alist[i+1]
           i ++
           ok = false
           count = 0
           set = new Set()
         }
       }
     }else{
     if(alist[i] == mae){
       set.add(alist[i])
       count = 2
       ok = true
       start = i-1
       res = Math.max(res,count)
     }else{
       mae = alist[i]
     }
     }
   }
   console.log(res)
  }
  Main(require("fs").readFileSync(0, "utf8"));
  

工夫した点

計算量に注意する必要があります。尺取法かそれもどきてきなものを使います。最初から順に見ていって、連続する2つのアルファベットを見つけたらokをtrueにして尺取法を開始します。もし被っているアルファベットが見つかったら末尾を削除していきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?