C
スコアは193です。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define LEFT 'L'
#define RIGHT 'R'
#define CAPS 'C'
#define UP 'U'
#define DOWN 'D'
#define SPACE 'Y'
#define CURSORLEFT 'B'
#define CURSORRIGHT 'F'
#define APPLY 'A'
#define TABLEX 10
#define TABLEY 4
#define casediff(A,B) (isupper(A)*islower(B)+islower(A)*isupper(B))
typedef struct {int x; int y;} coord;
coord letters[300];
#define initLetter(letter, X, Y) \
letters[letter].x=X, letters[letter].y=Y
#define initAllLetters(); \
initLetter('1',0,0);\
initLetter('1',0,0);\
initLetter('2',1,0);\
initLetter('2',1,0);\
initLetter('3',2,0);\
initLetter('3',2,0);\
initLetter('4',3,0);\
initLetter('4',3,0);\
initLetter('5',4,0);\
initLetter('5',4,0);\
initLetter('6',5,0);\
initLetter('6',5,0);\
initLetter('7',6,0);\
initLetter('7',6,0);\
initLetter('8',7,0);\
initLetter('8',7,0);\
initLetter('9',8,0);\
initLetter('9',8,0);\
initLetter('0',9,0);\
initLetter('0',9,0);\
initLetter('Q',0,1);\
initLetter('q',0,1);\
initLetter('W',1,1);\
initLetter('w',1,1);\
initLetter('E',2,1);\
initLetter('e',2,1);\
initLetter('R',3,1);\
initLetter('r',3,1);\
initLetter('T',4,1);\
initLetter('t',4,1);\
initLetter('Y',5,1);\
initLetter('y',5,1);\
initLetter('U',6,1);\
initLetter('u',6,1);\
initLetter('I',7,1);\
initLetter('i',7,1);\
initLetter('O',8,1);\
initLetter('o',8,1);\
initLetter('P',9,1);\
initLetter('p',9,1);\
initLetter('A',0,2);\
initLetter('a',0,2);\
initLetter('S',1,2);\
initLetter('s',1,2);\
initLetter('D',2,2);\
initLetter('d',2,2);\
initLetter('F',3,2);\
initLetter('f',3,2);\
initLetter('G',4,2);\
initLetter('g',4,2);\
initLetter('H',5,2);\
initLetter('h',5,2);\
initLetter('J',6,2);\
initLetter('j',6,2);\
initLetter('K',7,2);\
initLetter('k',7,2);\
initLetter('L',8,2);\
initLetter('l',8,2);\
initLetter('-',9,2);\
initLetter('-',9,2);\
initLetter('Z',0,3);\
initLetter('z',0,3);\
initLetter('X',1,3);\
initLetter('x',1,3);\
initLetter('C',2,3);\
initLetter('c',2,3);\
initLetter('V',3,3);\
initLetter('v',3,3);\
initLetter('B',4,3);\
initLetter('b',4,3);\
initLetter('N',5,3);\
initLetter('n',5,3);\
initLetter('M',6,3);\
initLetter('m',6,3);\
initLetter('_',7,3);\
initLetter('_',7,3);\
initLetter('@',8,3);\
initLetter('@',8,3);\
initLetter('.',9,3);\
initLetter('.',9,3);
typedef struct {int length; char instr[300];} movement;
movecasefold(char*instr,coord A, coord B){
register int i=0;int j;
if(A.x<B.x)
if(B.x-A.x<=TABLEX/2)
for(;B.x-A.x!=i;)instr[i++]=RIGHT;
else
for(;TABLEX-B.x+A.x!=i;)instr[i++]=LEFT;
else if(A.x>B.x)
if(A.x-B.x<=TABLEX/2)
for(;A.x-B.x!=i;)instr[i++]=LEFT;
else
for(;TABLEX-A.x+B.x!=i;)instr[i++]=RIGHT;
j=i;
if(A.y<B.y)
if(B.y-A.y<=TABLEY/2)
for(;B.y-A.y!=i-j;)instr[i++]=DOWN;
else
for(;TABLEY-B.y+A.y!=i-j;)instr[i++]=UP;
else if(A.y>B.y)
if(A.y-B.y<=TABLEY/2)
for(;A.y-B.y!=i-j;)instr[i++]=UP;
else
for(;TABLEY-A.y+B.y!=i-j;)instr[i++]=DOWN;
instr[i]='\0';
return i;
}
char sentence[50], oldcase, oldletter;
int sentencelength;
typedef struct {
int sentencetoorder[50];
int ordertosentence[50];
int length;
} order;
ordercopy(order*a, order b){
register int i;
for(i=0;++i<sentencelength;) a->sentencetoorder[i]=b.sentencetoorder[i], a->ordertosentence[i]=b.ordertosentence[i];
a->length=b.length;
}
order currentOrder;
movetwo(char*instr,int A, int B){
register int j; int i=0;
if(A<B)
{ for(j=A+1;j<B;j++)
if(currentOrder.sentencetoorder[j]<currentOrder.sentencetoorder[B])
instr[i++]=CURSORRIGHT;
}
else
{ for(j=A;j>B;j--)
if(currentOrder.sentencetoorder[j]<currentOrder.sentencetoorder[B])
instr[i++]=CURSORLEFT;
}
if(sentence[B]==' '){
instr[i++]=SPACE;
instr[i]='\0';
return i;
}
i+=movecasefold(instr+i,letters[oldletter],letters[sentence[B]]);
oldletter=sentence[B];
if(casediff(oldcase,sentence[B]))oldcase=sentence[B],instr[i++]=CAPS;
instr[i++]=APPLY;
instr[i]='\0';
return i;
}
moveall(char*instr){
int j;int i = 0;
oldcase='a';
oldletter='1';
for(j=0;++j<sentencelength;)
i+=movetwo(instr+i,currentOrder.ordertosentence[j-1],currentOrder.ordertosentence[j]);
return i;
}
iteration();
main(){
initAllLetters();
gets(sentence+1);*sentence='1';sentencelength=strlen(sentence);
int i;
for(i=0;++i<sentencelength;)currentOrder.sentencetoorder[i]=currentOrder.ordertosentence[i]=i;
char instr[300];
currentOrder.length=moveall(instr);
puts(instr);
while(iteration());
}
#define inside(item, start, stop) (((start)<=(item))&((item)<(stop)))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
iteration(){
char instr[300];
register int i;
int newstart, start, l;
static order oldorder, neworder;
ordercopy(&oldorder,currentOrder);
ordercopy(&neworder,currentOrder);
for(l=0;++l<sentencelength-1;)
for(start=0;++start<sentencelength-l;)
for(newstart=0;++newstart<sentencelength-l;)
if(start!=newstart){
for(i=0;++i<sentencelength;)
if(inside(i,start,start+l))currentOrder.ordertosentence[i-start+newstart]=oldorder.ordertosentence[i];
else if(inside(i,min(start,newstart),max(start+l,newstart+l)))currentOrder.ordertosentence[newstart<start?i+l:(i-l)]=oldorder.ordertosentence[i];
else currentOrder.ordertosentence[i]=oldorder.ordertosentence[i];
for(i=0;++i<sentencelength;) currentOrder.sentencetoorder[currentOrder.ordertosentence[i]]=i;
currentOrder.length=moveall(instr);
if(currentOrder.length<neworder.length){
puts(instr);
ordercopy(&neworder, currentOrder);
}
}
ordercopy(¤tOrder, neworder);
return neworder.length<oldorder.length;
}
「gcc virtualKeyboard.c」としてコンパイルします。引数 "./a.out"なしで実行します。入力をstdinから読み取り、出力をstdoutに書き込みます。