在Scala中找到下一个回文的更好算法

时间:2015-06-20 13:25:18

标签: algorithm scala

以下是问题:

如果从左到右和从右到左读取十进制系统中的表示相同,则正整数称为回文。对于给定的正整数K不超过1000000位,写入大于K的最小回文值输出。始终显示数字而不带前导零。

输入:第一行包含整数t,即测试用例的数量。整数K在下一行中给出。

输出:对于每个K,输出大于K的最小回文。示例

输入:

2 808 2133

输出:

818 2222

我的Undefined symbols for architecture x86_64: "QSqlDatabase::addDatabase(QString const&, QString const&)", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlDatabase::setDatabaseName(QString const&)", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlDatabase::defaultConnection", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlDatabase::open()", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlDatabase::QSqlDatabase()", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlDatabase::~QSqlDatabase()", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlDatabase::operator=(QSqlDatabase const&)", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlError::~QSqlError()", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlQuery::exec()", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlQuery::prepare(QString const&)", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlQuery::bindValue(QString const&, QVariant const&, QFlags<QSql::ParamTypeFlag>)", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlQuery::QSqlQuery(QString const&, QSqlDatabase)", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlQuery::~QSqlQuery()", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlDatabase::lastError() const", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlQuery::value(int) const", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "QSqlQuery::lastError() const", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o "operator<<(QDebug, QSqlError const&)", referenced from: authcontroller::authenticate(QString const&, QString const&) in authcontroller.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 代码:

Scala

我从object Pro_5 { // find the next palindrome larger than K def findPalindrome(input: String) = { // start from (k + 1) val tmp = (BigInt(input) + 1).toString val length = tmp.length val mid = length / 2 val left = tmp.substring(0, length >> 1) val right = tmp.substring(length >> 1, length) // whether continue for loop var flag = true // half of the result var res = "" for (i <- 0 until mid if flag) { if (left(i) > right(mid - 1)) { res = left flag = false } else if (left(i) < right(mid - 1)) { res = (BigInt(left) + 1).toString() flag = false } } if (length % 2 == 0) { res + res.reverse } else { res + tmp(mid) + res.reverse } } // get K def getInput(times: Int) = { for (time <- 0 until times) yield readLine() } def main(args: Array[String]) { // get compute times val times = readInt() getInput(times).map(findPalindrome(_)).foreach(println) } } 's answer

学到了一些东西

但是当我在Mark Peters中运行时,我仍然有一个SPOJ

你能帮我改进算法吗?

任何答案都会欢迎......

1 个答案:

答案 0 :(得分:1)

Mark Peters's answer确实已经解释了一个好的算法

所以这只是正确实施的问题。这里提示如何改进代码,减少java并支持奇数位数

def split(s: String) = {
  val mid = (s.length + 1) / 2
  val left = s.substring(0, mid)
  val right = s.substring(s.length - mid, s.length)
  (left, right)
}

//right string should be reversed
@tailrec
def compareFromIndex(left: String, right: String, i: Int): String = {
  if (i < 0) left
  if (left(i) > right(i)) left
  else if (left(i) < right(i)) (BigInt(left) + 1).toString()
  else compareFromIndex(left, right, i - 1)
}

val half = compareFromIndex(left, right.reverse, left.length - 1)

这几乎是完全实现:)只需正确输入分割功能。并从计算出的&#34;半&#34;中制作完整的回文。祝你好运!