結果
| 問題 |
No.1617 Palindrome Removal
|
| コンテスト | |
| ユーザー |
ccppjsrb
|
| 提出日時 | 2021-07-22 21:45:34 |
| 言語 | Go (1.23.4) |
| 結果 |
AC
|
| 実行時間 | 11 ms / 2,000 ms |
| コード長 | 2,415 bytes |
| コンパイル時間 | 20,610 ms |
| コンパイル使用メモリ | 231,528 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-17 17:08:25 |
| 合計ジャッジ時間 | 17,306 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 20 |
ソースコード
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
)
var iost *Iost
type Iost struct {
Scanner *bufio.Scanner
Writer *bufio.Writer
}
func NewIost(fp io.Reader, wfp io.Writer) *Iost {
const BufSize = 2000005
scanner := bufio.NewScanner(fp)
scanner.Split(bufio.ScanWords)
scanner.Buffer(make([]byte, BufSize), BufSize)
return &Iost{Scanner: scanner, Writer: bufio.NewWriter(wfp)}
}
func (i *Iost) Text() string {
if !i.Scanner.Scan() {
panic("scan failed")
}
return i.Scanner.Text()
}
func (i *Iost) Atoi(s string) int { x, _ := strconv.Atoi(s); return x }
func (i *Iost) GetNextInt() int { return i.Atoi(i.Text()) }
func (i *Iost) Atoi64(s string) int64 { x, _ := strconv.ParseInt(s, 10, 64); return x }
func (i *Iost) GetNextInt64() int64 { return i.Atoi64(i.Text()) }
func (i *Iost) Atof64(s string) float64 { x, _ := strconv.ParseFloat(s, 64); return x }
func (i *Iost) GetNextFloat64() float64 { return i.Atof64(i.Text()) }
func (i *Iost) Print(x ...interface{}) { fmt.Fprint(i.Writer, x...) }
func (i *Iost) Printf(s string, x ...interface{}) { fmt.Fprintf(i.Writer, s, x...) }
func (i *Iost) Println(x ...interface{}) { fmt.Fprintln(i.Writer, x...) }
func isLocal() bool { return os.Getenv("I") == "IronMan" }
func main() {
fp := os.Stdin
wfp := os.Stdout
if isLocal() {
fp, _ = os.Open(os.Getenv("END_GAME"))
}
iost = NewIost(fp, wfp)
defer func() {
if isLocal() {
iost.Println(recover())
}
iost.Writer.Flush()
}()
solve()
for i := 0; isLocal() && i < 100; i++ {
iost.Println("-----------------------------------")
solve()
}
}
func solve() {
s := iost.Text()
n := len(s)
if !isPalindrome(s) {
iost.Println(n)
return
}
cnt := make([]int, 26)
for i := 0; i < n; i++ {
cnt[s[i]-'a']++
}
odd := make([]int, 0)
even := make([]int, 0)
for i := 0; i < 26; i++ {
if cnt[i] == 0 {
continue
}
if cnt[i]%2 == 0 {
even = append(even, cnt[i])
continue
}
odd = append(odd, cnt[i])
}
switch {
case len(even)+len(odd) == 1:
if n%2 == 0 {
iost.Println(0)
return
}
iost.Println(-1)
case n == 3:
iost.Println(-1)
default:
iost.Println(n - 2)
}
}
func isPalindrome(s string) bool {
l := 0
r := len(s) - 1
for l < r {
if s[l] != s[r] {
return false
}
l++
r--
}
return true
}
ccppjsrb