JavaScript(ES6),3.143(已保存81个字节,664个字节的程序)
R='replace',S=String.fromCharCode,T=c=>c.charCodeAt(),U='toUpperCase',V='0000000',W=(a,b,c=2)=>a.toString(c).slice(b),X=x=>'0b'+x,Y=a=>[...a].reverse().join``,Z=/[^]/g
C=s=>S(...((Y(q=s[U]()[R](/[^A-Z]/g,m=''))==q?(q=q.slice(0,p=-~q.length/2),p%1&&10):11)+q[R](Z,x=>W(T(x),2))+111+s[R](Z,c=>/[a-z]/.test(c)?W("00",m,m=1):m+(/[A-Z]/.test(c,m='')?"01":W(c<'!'?2:T(c)+384)))+V).match(/(?!0+$).{8}/g).map(X))
D=s=>{s=s[R](Z,c=>W(256+T(c),1))+V;M=r=>(s=s[R](p=s.match(`^${r}|`)[0],''),p);for([,a]=M`1.|0`,t=u=i='';!M`111`;)t+=W(X(M`.{5}`)-~8,0,36);for(t+=W(Y(t),a?a/0:1);p;)u+=M`0(?=00)|00?1`?(c=t[i++])?+p[1]?c[U]():c:'':M`10`?' ':M`11`&&S(X(M`.{7}`));return u+W(t,i)}
现在,我对该程序(和计分系统)非常满意,我将写一些解释。
基本思想是将输入压缩为一串位,然后将每组8位压缩为一个字节。为了说明的目的,我将只操作位字符串。
该位串可以分为几个部分:
input -> Taco Cat.
output -> 0101000000100011011111110100001100100011101011100000000
0 | 10100 00001 00011 01111 111 | 01 00001 10 01 0001 110101110
header | letter data | styling data
标头是一个非常简单的映射:
0 -> odd-length palindrome
10 -> even-length palindrome
11 -> non-palindrome
信函数据也相当简单。首先,从字符串中提取所有非字母,并将所有字母都转换为大写。如果生成的字符串是回文,则将反面的一半剥离。然后应用此映射:
A -> 00001
B -> 00010
C -> 00011
D -> 00100
...
Z -> 11010
本部分以结尾111
。之后是样式数据,该数据存储大写/小写数据和非字母数据。这是这样的:
01 -> next letter as uppercase
0...01 (n 0s) -> next (n-1) letters as lowercase
10 -> space
11xxxxxxx -> character with code point 0bxxxxxxx
因此,通过上面显示的示例,我们有
header: 0 -> palindrome
letter data: 10100 00001 00011 01111 111 -> taco
styling data:
01 -> T
00001 -> aco
10 -> <space>
01 -> C
0001 -> at
110101110 -> .
当到达位字符串的末尾时,字母数据中所有剩余的字符都将附加到结果中。这使我们不必进行最后的操作,000...001
并允许我们截断字符串中的这些位。
通过测试用例:
tacocat -> 3 bytes (-4)
24 bits: 010100000010001101111111
toohottohoot -> 5 bytes (-7)
35 bits: 10101000111101111010000111110100111
todderasesareddot -> 7 bytes (-10)
49 bits: 0101000111100100001000010110010000011001100101111
amanaplanacanalpanama -> 8 bytes (-13)
59 bits: 00000101101000010111000001100000110000001011100000100011111
wasitacaroracatisaw? -> 11 bytes (-9)
84 bits: 010111000011001101001101000000100011000011001001111111000000000000000000001110111111
Bob -> 2 bytes (-1)
16 bits: 0000100111111101
IManAmRegalAGermanAmI -> 13 bytes (-8)
98 bits: 00100101101000010111000001011011001000101001110000101100111010100010100101000001010100000010100101
DogeeseseeGod -> 7 bytes (-6)
54 bits: 000100011110011100101001011001100101111010000000000101
A Santa at NASA -> 8 bytes (-7)
63 bits: 100000110011000010111010100000011110110010000011000011001010101
Go hang a salami! I'm a lasagna hog. -> 20 bytes (-16)
154 bits: 1000111011110100000001011100011100001100110000101100000010110101001111010011000000110001100000000111010000110011101001110011000110000000001100000111010111
wasitacaroraratisaw?
是一个反例