C,259231字节
高尔夫守则
#define v a[1][i
i,k,l,x,h,w;main(char*s,char**a){for(;v];w+=2*!x,s=v++],h=x>h?x:h)x=(s==v])*(x+1);h++;s=malloc((x=h++*++w+1)+w);memset(s,32,h*w);for(i=k;v];s[x+1]=s[x]=k=v++],x=k==v]?x-w:(h-1)*w+l++*2+3)s[i*w]=10;printf("%s",s);}
详细代码
//Variable Explanations:
//i - increment through argument string, must beinitialized to 0
//k - increment through argument string, must be initialized to 0
//l - record x coordinate in return value, must be initialized to 0
//x - record the actual character position within the return string
//arrheight - the height of the return string
//arrwidth - the width of the return string
//arr - the return string
//argv - the string containing the arguments
#define v argv[1][i
i,k,l,x,arrheight,arrwidth;
main(char*arr,char**argv){
for(;v]; //For Length of input
arrwidth+=2*!x, //increment width by 2 if this char is not the same as the last
arr=v++], //set arr to current char
arrheight=x>arrheight?x:arrheight //see if x is greater than the largest recorded height
)x=(arr==v])*(x+1); //if this character is the same as the last, increment x (using arr to store previous char)
arrheight++; //increment height by one since its 0 indexed
arr=malloc((x=arrheight++*++arrwidth+1)+arrwidth); //create a flattened array widthxheight and set x to be the bottom left position
memset(arr,32,arrheight*arrwidth); //fill array with spaces
for(i=k;v]; //For Length of input
arr[x+1]=arr[x]=k=v++], //set x and x+1 positions to the current character, store current character in i
x=k==v]?x-arrwidth:(arrheight-1)*arrwidth+l++*2+3 //if next char is same as current move vertically, else set x to bottom of next column
)arr[i*arrwidth]=10; //Add new lines to string at end of width
printf("%s",arr); //output string
}
与GCC一起编译,无特殊标志
编辑
感谢adelphus,节省了28个字节。他的更改使我可以创建一个定义。我将while循环放入for循环中,通过重新排列循环来节省2个字节。我还解决了一个问题,当输入的最后一个字符不是单例时,代码会中断。如果只有一个唯一的字母,则该代码将失败,但在所有其他情况下都可以使用。