Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

No.701 ひとりしりとり【yukicoder解説】

Last updated at Posted at 2018-10-24

初めて回答の解説を書いてみる。
他の方の回答を見てみたけど、同じアプローチの方はいらっしゃらなそうなので。
しかし、めちゃくちゃ愚直。

回答

Node.js
const rl=require("readline").createInterface(process.stdin,process.stdout);

rl.once("line",n=>{
	for(var i=1;i<=(0|n);i=0|i+1){
		console.log("a"+(""+i)
		.replace(/0/g,"a")
		.replace(/1/g,"b")
		.replace(/2/g,"c")
		.replace(/3/g,"d")
		.replace(/4/g,"e")
		.replace(/5/g,"f")
		.replace(/6/g,"g")
		.replace(/7/g,"h")
		.replace(/8/g,"i")
		.replace(/9/g,"j")
		+(i==(0|n)? "n": "a"));
	}
	process.exit();
});

解説

しりとりとは名ばかりで、

  1. i行目の最後とi+1行目の最初の文字列の一致
  2. 全ての行に重複しない
  3. n行目の最後はnとなる
    の内容さえ守れればどのような文字列を出力しても問題ない。
    そこで先頭と最後の文字を"a"に固定して重複しない文字列を作る(最後だけ"n")。

重複しない文字列を最大100000個生成する必要があるが、
数字を変換した"1"-"100000"の文字列を利用すれば簡単に用意ができる。
あとはその数字の"0"-"9"を適当なアルファベットに置き換えて左右に"a"とか"n"をくっつけて出力すれば完了。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?