0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

POH6+『「え、妻が松江?」松江Ruby会議07協賛 回文作成プログラミングコンテスト』 言語毎のコード一覧

Posted at

概要

結局全言語でコードをうpしたので記念に貼っておきます。まあ練度が低いのでランキングは4~5位程度でしたが……。

Ruby

code.rb
n=gets;w=ARGF.read.split.sort;c="";w.select!{|x|c+=x if(y=x.reverse)==x;x<y&&w.index(y)};s=w.join;puts s+c+s.reverse

Java

code.java
import java.io.*;import java.util.*;class Main{public static void main(String[]args)throws Exception{BufferedReader a=new BufferedReader(new InputStreamReader(System.in));String t=a.readLine(),v,c="";StringBuffer d,l=new StringBuffer();TreeSet w = new TreeSet();while((t=a.readLine())!=null)w.add(t);Iterator b=w.iterator();while(b.hasNext()){v=(String)b.next();d=new StringBuffer(v);t=d.reverse().toString();if(v.equals(t))c+=v;if(v.compareTo(t)<0&&w.contains(t))l.append(v);}if(c.length()==5)c+=c+c+c+c+c+c;System.out.println(l+c+l.reverse());}}

PHP

code.php
<?php fscanf(STDIN,"%d\n",$n);$w=array();while(!feof(STDIN)){$w[]=trim(fgets(STDIN));}$c="";$l="";sort($w);foreach($w as $v){$r=strrev($v);if($r==$v){$c.=$v;}elseif($v<$r&&array_search($r,$w)){$l.=$v;}}echo $l.$c.strrev($l);?>

Python

code.py
n=int(input());w=[];exec('w+=[raw_input()];'*n);C=L="";w.sort();
for V in w:
	R=V[::-1]
	if V==R:C+=V
	if V<R and R in w:L+=V
print(L+C+L[::-1])

Perl

code.pl
$n = <STDIN>;while (<STDIN>){chomp($_);push(@w,$_);}$C=$L="";@w=sort(@w);foreach(@w){$T=reverse($_);$C=$C.$T if $T eq $_;if($_ lt $T&&grep{$_ eq $T}@w){$L=$L.$_;}}print $L.$C.reverse($L);

C

code.c
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# define f strcmp
char W[11],T[11],L[5001],C[10001],R[5001],w[1000][11];int j,l,N,i,c=0;void F(char *s,char *d){l=strlen(s);for(j=0;j<l;++j)d[j]=s[l - j - 1];d[l]='\0';}int main(void){scanf("%d",&N);for(i=0;i<N;++i){scanf("%s",W);F(W,T);if(f(W,T))strcpy(w[c],W),++c;else strcat(C,W);}qsort(w,c,11,f);for(i=0;i<c;++i){F(w[i],T);if(f(w[i],T)<0 &&bsearch(T,w,c,11,f))strcat(L,w[i]);}F(L,R);printf("%s%s%s\n",L,C,R);}

C++

code.cpp
# include<iostream>
# include<set>
# include<string>
using namespace std;int main(void){string n,T,L="",C="",R="";multiset<string>w;cin>>n;while(cin>>T)w.insert(T);for(auto W:w){T=W;reverse(T.begin(),T.end());if(W==T)C+=W;if(W<T&&w.count(T)){L+=W;R=T+R;}}cout<<L+C+R<<endl;}

C#

code.cs
using System;class a{static void Main(){string n=Console.ReadLine(),l="",c="",r="",v;string[]w=Console.In.ReadToEnd().Split('\n');Array.Sort(w);char[]u;foreach(var t in w){u=t.ToCharArray();Array.Reverse(u);v=new string(u);if(t==v)c+=t;if(t.CompareTo(v)<0&&Array.BinarySearch(w,v)>=0){l+=t;r=v+r;}}Console.WriteLine(l+c+r);}}

JavaScript

code.js
process.stdin.resume(),process.stdin.setEncoding("utf8"),process.stdin.on("data",function(s){for(w=s.split("\n"),n=parseInt(w[0]),w.shift(),w.sort(),T=L=C="",i=1;i<=n;i++)T=w[i].split("").reverse().join(""),T==w[i]?C+=T:w[i]<T&&w.indexOf(T)>=0&&(L+=w[i]);console.log(L+C+L.split("").reverse().join(""))});

0
1
1

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?