Code Golf:字符串中的重复字符删除

发布于 2024-08-04 00:22:07 字数 525 浏览 2 评论 0原文

挑战:按字符数计算最短的代码,检测并删除字符串中的重复字符。删除包括重复字符的所有实例(因此,如果找到 3 个 n,则所有三个都必须删除),并且需要保留原始字符顺序。

示例输入 1:
nbHHkRvrXbvkn

示例输出 1:
RRX


示例输入 2:
nbHHkRbvnrXbvkn

示例输出 2:
RRX

(第二个示例删除出现三次的字母;某些解决方案未能解决此问题)

(这是基于 我的另一个问题,我需要在 C# 中以最快的方式执行此操作,但我认为它可以在跨语言中实现良好的 Code Golf。)

The challenge: The shortest code, by character count, that detects and removes duplicate characters in a String. Removal includes ALL instances of the duplicated character (so if you find 3 n's, all three have to go), and original character order needs to be preserved.

Example Input 1:
nbHHkRvrXbvkn

Example Output 1:
RrX


Example Input 2:
nbHHkRbvnrXbvkn

Example Output 2:
RrX

(the second example removes letters that occur three times; some solutions have failed to account for this)

(This is based on my other question where I needed the fastest way to do this in C#, but I think it makes good Code Golf across languages.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

合久必婚 2024-08-11 00:22:07

LabVIEW 7.1

ONE 字符,即程序框图中的蓝色常量“1”。
我发誓,输入是复制粘贴的;-)

http://i25.tinypic.com/hvc4mp.png

< a href="http://i26.tinypic.com/5pnas.png">http://i26.tinypic.com/5pnas.png

LabVIEW 7.1

ONE character and that is the blue constant '1' in the block diagram.
I swear, the input was copy and paste ;-)

http://i25.tinypic.com/hvc4mp.png

http://i26.tinypic.com/5pnas.png

み零 2024-08-11 00:22:07

Perl

Perl 的 21 个字符,31 个要调用的字符,总共 36 次击键(计算移位和最后的回车):

perl -pe's/$1//gwhile/(.).*\1/'

Perl

21 characters of perl, 31 to invoke, 36 total keystrokes (counting shift and final return):

perl -pe's/$1//gwhile/(.).*\1/'
彼岸花ソ最美的依靠 2024-08-11 00:22:07

Ruby — 61 53 51 56 35

61 个字符,标尺说。 (给了我另一个代码高尔夫的想法......)

  puts ((i=gets.split(''))-i.select{|c|i.to_s.count(c)<2}).join
+-------------------------------------------------------------------------+
||    |    |    |    |    |    |    |    |    |    |    |    |    |    |  |
|0         10        20        30        40        50        60        70 |
|                                                                         |
+-------------------------------------------------------------------------+
  gets.chars{|c|
gt;<<c[$_.count(c)-1]}

... 35 by Nakilon

Ruby — 61 53 51 56 35

61 chars, the ruler says. (Gives me an idea for another code golf...)

  puts ((i=gets.split(''))-i.select{|c|i.to_s.count(c)<2}).join
+-------------------------------------------------------------------------+
||    |    |    |    |    |    |    |    |    |    |    |    |    |    |  |
|0         10        20        30        40        50        60        70 |
|                                                                         |
+-------------------------------------------------------------------------+
  gets.chars{|c|
gt;<<c[$_.count(c)-1]}

... 35 by Nakilon

恋竹姑娘 2024-08-11 00:22:07

APL

23 个字符:

(((1+ρx)-(ϕx)ιx)=xιx)/x

我是 APL 新手(昨天才学的),所以要友善 - 这当然不是最有效的方法。我很遗憾我没有远远击败 Perl。

话又说回来,也许它说明了一些问题,对于新手来说,在 APL 中解决这个问题的最自然的方式仍然比迄今为止任何语言的任何其他解决方案都更简洁。

APL

23 characters:

(((1+ρx)-(ϕx)ιx)=xιx)/x

I'm an APL newbie (learned it yesterday), so be kind -- this is certainly not the most efficient way to do it. I'm ashamed I didn't beat Perl by very much.

Then again, maybe it says something when the most natural way for a newbie to solve this problem in APL was still more concise than any other solution in any language so far.

时光是把杀猪刀 2024-08-11 00:22:07

Python:

s=raw_input()
print filter(lambda c:s.count(c)<2,s)

这是一个完整的工作程序,从控制台读取和写入。单行版本可以直接从命令行使用

python -c 's=raw_input();print filter(lambda c:s.count(c)<2,s)'

Python:

s=raw_input()
print filter(lambda c:s.count(c)<2,s)

This is a complete working program, reading from and writing to the console. The one-liner version can be directly used from the command line

python -c 's=raw_input();print filter(lambda c:s.count(c)<2,s)'
-黛色若梦 2024-08-11 00:22:07

J (16 12 个字符)

(~.{~[:I.1=#/.~)

例:

(~.{~[:I.1=#/.~) 'nbHHkRvrXbvkn'
    RrX

只需加括号即可默认执行。如果放入动词中,实际代码本身将是 14 个字符。

当然有更聪明的方法可以做到这一点。

编辑:所讨论的更聪明的方式:

(~.#~1=#/.~) 'nbHHkRvrXbvkn'
    RrX

12 个字符,如果设置在动词中则只有 10 个字符。我仍然讨厌这样的事实:它要遍历列表两次,一次是为了计数(#/.),另一次是为了返回唯一值(nub 或 ~.),但即使是 nubcount(“misc”库中的标准动词)也会执行两次。

J (16 12 characters)

(~.{~[:I.1=#/.~)

Example:

(~.{~[:I.1=#/.~) 'nbHHkRvrXbvkn'
    RrX

It only needs the parenthesis to be executed tacitly. If put in a verb, the actual code itself would be 14 characters.

There certainly are smarter ways to do this.

EDIT: The smarter way in question:

(~.#~1=#/.~) 'nbHHkRvrXbvkn'
    RrX

12 characters, only 10 if set in a verb. I still hate the fact that it's going through the list twice, once to count (#/.) and another to return uniques (nub or ~.), but even nubcount, a standard verb in the 'misc' library does it twice.

时光沙漏 2024-08-11 00:22:07

Haskell

在 Haskell 中肯定有更短的方法来做到这一点,但是:

Prelude Data.List> let h y=[x|x<-y,(<2).length$filter(==x)y]
Prelude Data.List> h "nbHHkRvrXbvkn"
"RrX"

忽略 let,因为它只在 GHCi 中的函数声明中需要,我们有 hy=[x|x<-y,(<2).length$ filter(==x)y],为 37 个字符(这与 "".join(c for c in s if s.count(c)<2) 的当前“核心”Python 相关),而且它实际上是相同的代码)。

如果你想用它制作一个完整的程序,

h y=[x|x<-y,(<2).length$filter(==x)y]
main=interact h

$ echo "nbHHkRvrXbvkn" | runghc tmp.hs
RrX

$ wc -c tmp.hs
54 tmp.hs

或者我们可以这样删除一个字符:

main=interact(\y->[x|x<-y,(<2).length$filter(==x)y])

$ echo "nbHHkRvrXbvkn" | runghc tmp2.hs
RrX

$ wc -c tmp2.hs
53 tmp2.hs

它在标准输入的所有上运行,而不是逐行运行,但这在我看来是可以接受的。

Haskell

There's surely shorter ways to do this in Haskell, but:

Prelude Data.List> let h y=[x|x<-y,(<2).length$filter(==x)y]
Prelude Data.List> h "nbHHkRvrXbvkn"
"RrX"

Ignoring the let, since it's only required for function declarations in GHCi, we have h y=[x|x<-y,(<2).length$filter(==x)y], which is 37 characters (this ties the current "core" Python of "".join(c for c in s if s.count(c)<2), and it's virtually the same code anyway).

If you want to make a whole program out of it,

h y=[x|x<-y,(<2).length$filter(==x)y]
main=interact h

$ echo "nbHHkRvrXbvkn" | runghc tmp.hs
RrX

$ wc -c tmp.hs
54 tmp.hs

Or we can knock off one character this way:

main=interact(\y->[x|x<-y,(<2).length$filter(==x)y])

$ echo "nbHHkRvrXbvkn" | runghc tmp2.hs
RrX

$ wc -c tmp2.hs
53 tmp2.hs

It operates on all of stdin, not line-by-line, but that seems acceptable IMO.

演出会有结束 2024-08-11 00:22:07

C89(106 个字符)

这个使用了与我原来的答案完全不同的方法。有趣的是,写完后,然后查看另一个答案,我看到方法非常相似。感谢咖啡馆在我之前提出了这个方法。

b[256];l;x;main(c){while((c=getchar())>=0)b[c]=b[c]?1:--l;
for(;x-->l;)for(c=256;c;)b[--c]-x?0:putchar(c);}

在一行中,它是 58+48 = 106 字节

C89(173 个字符)

这是我原来的答案。正如评论中所说,它工作得不太好......

#include<stdio.h>
main(l,s){char*b,*d;for(b=l=s=0;l==s;s+=fread(b+s,1,9,stdin))b=realloc(b,l+=9)
;d=b;for(l=0;l<s;++d)if(!memchr(b,*d,l)&!memchr(d+1,*d,s-l++-1))putchar(*d);}

在两行中,它是 17+1+78+77 = 173 字节

C89 (106 characters)

This one uses a completely different method than my original answer. Interestingly, after writing it and then looking at another answer, I saw the methods were very similar. Credits to caf for coming up with this method before me.

b[256];l;x;main(c){while((c=getchar())>=0)b[c]=b[c]?1:--l;
for(;x-->l;)for(c=256;c;)b[--c]-x?0:putchar(c);}

On one line, it's 58+48 = 106 bytes.

C89 (173 characters)

This was my original answer. As said in the comments, it doesn't work too well...

#include<stdio.h>
main(l,s){char*b,*d;for(b=l=s=0;l==s;s+=fread(b+s,1,9,stdin))b=realloc(b,l+=9)
;d=b;for(l=0;l<s;++d)if(!memchr(b,*d,l)&!memchr(d+1,*d,s-l++-1))putchar(*d);}

On two lines, it's 17+1+78+77 = 173 bytes.

苍暮颜 2024-08-11 00:22:07

C#

65 个字符:

new String(h.Where(x=>h.IndexOf(x)==h.LastIndexOf(x)).ToArray());

67 个重新分配的字符:

h=new String(h.Where(x=>h.IndexOf(x)==h.LastIndexOf(x)).ToArray());

C#

65 Characters:

new String(h.Where(x=>h.IndexOf(x)==h.LastIndexOf(x)).ToArray());

67 Characters with reassignment:

h=new String(h.Where(x=>h.IndexOf(x)==h.LastIndexOf(x)).ToArray());
彩扇题诗 2024-08-11 00:22:07

C#

new string(input.GroupBy(c => c).Where(g => g.Count() == 1).ToArray());

71 个字符

C#

new string(input.GroupBy(c => c).Where(g => g.Count() == 1).ToArray());

71 characters

呢古 2024-08-11 00:22:07

PHP(136 个字符)

<?PHP
function q($x){return $x<2;}echo implode(array_keys(array_filter(
array_count_values(str_split(stream_get_contents(STDIN))),'q')));

在一行中,它是 5+1+65+65 = 136 字节。使用 PHP 5.3,您可以节省一些字节,使函数匿名,但我现在无法测试。也许类似于:

<?PHP
echo implode(array_keys(array_filter(array_count_values(str_split(
stream_get_contents(STDIN))),function($x){return $x<2;})));

5+1+66+59 = 131 字节。

PHP (136 characters)

<?PHP
function q($x){return $x<2;}echo implode(array_keys(array_filter(
array_count_values(str_split(stream_get_contents(STDIN))),'q')));

On one line, it's 5+1+65+65 = 136 bytes. Using PHP 5.3 you could save a few bytes making the function anonymous, but I can't test that now. Perhaps something like:

<?PHP
echo implode(array_keys(array_filter(array_count_values(str_split(
stream_get_contents(STDIN))),function($x){return $x<2;})));

That's 5+1+66+59 = 131 bytes.

梦在深巷 2024-08-11 00:22:07

另一个 APL 解决方案

作为动态函数(18 个字符)

{(1+=/¨(ω∘∊¨ω))/ω}

行,假设输入位于变量 x(16 个字符)中:

(1+=/¨(x∘∊¨x))/x

another APL solution

As a dynamic function (18 charachters)

{(1+=/¨(ω∘∊¨ω))/ω}

line assuming that input is in variable x (16 characters):

(1+=/¨(x∘∊¨x))/x
灯角 2024-08-11 00:22:07

VB.NET

For Each c In s : s = IIf(s.LastIndexOf(c) <> s.IndexOf(c), s.Replace(CStr(c), Nothing), s) : Next

诚然,VB 不是尝试保存字符的最佳语言,但该行有 98 个字符。

VB.NET

For Each c In s : s = IIf(s.LastIndexOf(c) <> s.IndexOf(c), s.Replace(CStr(c), Nothing), s) : Next

Granted, VB is not the optimal language to try to save characters, but the line comes out to 98 characters.

§对你不离不弃 2024-08-11 00:22:07

PowerShell

61 个字符。其中 $s="nbHHkRvrXbvkn"$a 是结果。

$h=@{}
($c=[char[]]$s)|%{$h[$_]++}
$c|%{if($h[$_]-eq1){$a+=$_}}

功能齐全的参数化脚本:

param($s)
$h=@{}
($c=[char[]]$s)|%{$h[$_]++}
$c|%{if($h[$_]-eq1){$a+=$_}}
$a

PowerShell

61 characters. Where $s="nbHHkRvrXbvkn" and $a is the result.

$h=@{}
($c=[char[]]$s)|%{$h[$_]++}
$c|%{if($h[$_]-eq1){$a+=$_}}

Fully functioning parameterized script:

param($s)
$h=@{}
($c=[char[]]$s)|%{$h[$_]++}
$c|%{if($h[$_]-eq1){$a+=$_}}
$a
如梦 2024-08-11 00:22:07

C:83 89 93 99 101 个字符

  • O(n2) 时间。
  • 限制为 999 个字符。
  • 仅适用于 32 位模式(由于未使用 #include-ing (花费 18 个字符)使得 gets< 的返回类型/code> 被解释为 int 并砍掉一半的地址位)。
  • 显示友好的“警告:该程序使用 gets(),这是不安全的。”在 Mac 上。

main(){char s[999],*c=gets(s);for(;*c;c++)strchr(s,*c)-strrchr(s,*c)||putchar(*c);}

(这个类似的 82-chars 版本通过命令行获取输入

main(char*c,char**S){for(c=*++S;*c;c++)strchr(*S,*c)-strrchr(*S,*c)||putchar(*c);}

:)

C: 83 89 93 99 101 characters

  • O(n2) time.
  • Limited to 999 characters.
  • Only works in 32-bit mode (due to not #include-ing <stdio.h> (costs 18 chars) making the return type of gets being interpreted as an int and chopping off half of the address bits).
  • Shows a friendly "warning: this program uses gets(), which is unsafe." on Macs.

.

main(){char s[999],*c=gets(s);for(;*c;c++)strchr(s,*c)-strrchr(s,*c)||putchar(*c);}

(and this similar 82-chars version takes input via the command line:

main(char*c,char**S){for(c=*++S;*c;c++)strchr(*S,*c)-strrchr(*S,*c)||putchar(*c);}

)

一袭白衣梦中忆 2024-08-11 00:22:07

Golfscript(符号) - 15

  .`{\{=}+,,(!}+,
+-------------------------------------------------------------------------+
||    |    |    |    |    |    |    |    |    |    |    |    |    |    |  |
|0         10        20        30        40        50        60        70 |
|                                                                         |
+-------------------------------------------------------------------------+

Golfscript(sym) - 15

  .`{\{=}+,,(!}+,
+-------------------------------------------------------------------------+
||    |    |    |    |    |    |    |    |    |    |    |    |    |    |  |
|0         10        20        30        40        50        60        70 |
|                                                                         |
+-------------------------------------------------------------------------+
夕嗳→ 2024-08-11 00:22:07

Haskell

(只是删除了 Mark Rushakoff 的几个字符,我宁愿将其作为对他的评论发布)

h y=[x|x<-y,[_]<-[filter(==x)y]]

这是更好的 Haskell 习惯用法,但对于非 Haskell 人来说可能比这更难理解:

h y=[z|x<-y,[z]<-[filter(==x)y]]

编辑为 hiena 和其他人添加解释:

我假设您理解 Mark 的版本,所以我只介绍更改。 Mark 的表达式:

(<2).length $ filter (==x) y

过滤 y 以获取 == x 的元素列表,找到该列表的长度并确保它小于 2。 (事实上​​,它的长度必须为一,但 ==1<2 长)我的版本:

[z] <- [filter(==x)y]

执行相同的过滤器,然后放入结果列表到列表中作为唯一元素。现在箭头(看起来像集合包含!)表示“依次对于 RHS 列表的每个元素,调用该元素 [z]”。 [z]是包含单个元素z的列表,因此元素“filter(==x)y”只能称为“ [z]" 如果它只包含一个元素。否则它会被丢弃并且永远不会用作 z 的值。因此,z(在列表理解中返回于 | 的左侧)正是 x 的组成部分filter 返回长度为 1 的列表。

这是我的第二个版本,我的第一个版本返回 x 而不是 z - 因为它们无论如何都是相同的 - 并将 z 重命名为 >_ 这是 Haskell 符号,表示“这个值不会被使用,所以我不会通过给它一个名字来使我的代码复杂化”。

Haskell

(just knocking a few characters off Mark Rushakoff's effort, I'd rather it was posted as a comment on his)

h y=[x|x<-y,[_]<-[filter(==x)y]]

which is better Haskell idiom but maybe harder to follow for non-Haskellers than this:

h y=[z|x<-y,[z]<-[filter(==x)y]]

Edit to add an explanation for hiena and others:

I'll assume you understand Mark's version, so I'll just cover the change. Mark's expression:

(<2).length $ filter (==x) y

filters y to get the list of elements that == x, finds the length of that list and makes sure it's less than two. (in fact it must be length one, but ==1 is longer than <2 ) My version:

[z] <- [filter(==x)y]

does the same filter, then puts the resulting list into a list as the only element. Now the arrow (meant to look like set inclusion!) says "for every element of the RHS list in turn, call that element [z]". [z] is the list containing the single element z, so the element "filter(==x)y" can only be called "[z]" if it contains exactly one element. Otherwise it gets discarded and is never used as a value of z. So the z's (which are returned on the left of the | in the list comprehension) are exactly the x's that make the filter return a list of length one.

That was my second version, my first version returns x instead of z - because they're the same anyway - and renames z to _ which is the Haskell symbol for "this value isn't going to be used so I'm not going to complicate my code by giving it a name".

嘦怹 2024-08-11 00:22:07

Javascript 1.8

s.split('').filter(function (o,i,a) a.filter(function(p) o===p).length <2 ).join('');

或类似的 python 示例:

[s[c] for (c in s) if (s.split("").filter(function(p) s[c]===p).length <2)].join('');

Javascript 1.8

s.split('').filter(function (o,i,a) a.filter(function(p) o===p).length <2 ).join('');

or alternately- similar to the python example:

[s[c] for (c in s) if (s.split("").filter(function(p) s[c]===p).length <2)].join('');
对你再特殊 2024-08-11 00:22:07

TCL

123 个字符。也许可以缩短它,但这对我来说已经足够了。

proc h {i {r {}}} {foreach c [split $i {}] {if {[llength [split $i $c]]==2} {set r $r$c}}
return $r}
puts [h [gets stdin]]

TCL

123 chars. It might be possible to get it shorter, but this is good enough for me.

proc h {i {r {}}} {foreach c [split $i {}] {if {[llength [split $i $c]]==2} {set r $r$c}}
return $r}
puts [h [gets stdin]]
叹倦 2024-08-11 00:22:07

C

完整的 C 程序,141 字节(包括换行符)。

#include<stdio.h>
c,n[256],o,i=1;main(){for(;c-EOF;c=getchar())c-EOF?n[c]=n[c]?-1:o++:0;for(;i<o;i++)for(c=0;c<256;c++)n[c]-i?0:putchar(c);}

C

Full program in C, 141 bytes (counting newlines).

#include<stdio.h>
c,n[256],o,i=1;main(){for(;c-EOF;c=getchar())c-EOF?n[c]=n[c]?-1:o++:0;for(;i<o;i++)for(c=0;c<256;c++)n[c]-i?0:putchar(c);}
背叛残局 2024-08-11 00:22:07

Scala

54 个字符仅用于方法主体,66 个字符用于(静态类型)方法声明:

def s(s:String)=(""/:s)((a,b)=>if(s.filter(c=>c==b).size>1)a else a+b)

Scala

54 chars for the method body only, 66 with (statically typed) method declaration:

def s(s:String)=(""/:s)((a,b)=>if(s.filter(c=>c==b).size>1)a else a+b)
时光磨忆 2024-08-11 00:22:07

Ruby

63 个字符。

puts (t=gets.split(//)).map{|i|t.count(i)>1?nil:i}.compact.join

Ruby

63 chars.

puts (t=gets.split(//)).map{|i|t.count(i)>1?nil:i}.compact.join
又爬满兰若 2024-08-11 00:22:07

VB.NET / LINQ

完整工作语句 96 个字符

Dim p=New String((From c In"nbHHkRvrXbvkn"Group c By c Into i=CountWhere i=1 Select c).ToArray)

完成工作语句,带有原始字符串和关闭 VB 特定的“漂亮列表(代码重新格式化”),长度为 96 个字符,非工作语句,没有原始字符串,长度为 84 个字符。

(请在回答之前确保您的代码可以工作。谢谢。)

VB.NET / LINQ

96 characters for complete working statement

Dim p=New String((From c In"nbHHkRvrXbvkn"Group c By c Into i=Count Where i=1 Select c).ToArray)

Complete working statement, with original string and the VB Specific "Pretty listing (reformatting of code" turned off, at 96 characters, non-working statement without original string at 84 characters.

(Please make sure your code works before answering. Thank you.)

想你只要分分秒秒 2024-08-11 00:22:07

C

(第一个版本:112 个字符;第二个版本:107 个字符)

k[256],o[100000],p,c;main(){while((c=getchar())!=-1)++k[o[p++]=c];for(c=0;c<p;c++)if(k[o[c]]==1)putchar(o[c]);}

那是

/* #include <stdio.h> */
/* int */ k[256], o[100000], p, c;
/* int */ main(/* void */) {
  while((c=getchar()) != -1/*EOF*/) {
    ++k[o[p++] = /*(unsigned char)*/c];
  }
  for(c=0; c<p; c++) {
    if(k[o[c]] == 1) {
      putchar(o[c]);
    }
  }
  /* return 0; */
}

因为 getchar() 返回 int 而 putchar 接受 int,因此可以“安全”删除 #include。
如果没有包含,则未定义 EOF,因此我使用 -1 代替(并获得了一个字符)。
该程序仅适用于少于 100000 个字符的输入!

版本 2,感谢 strager
107 个字符

#ifdef NICE_LAYOUT
#include <stdio.h>

/* global variables are initialized to 0 */
int char_count[256];                          /* k in the other layout */
int char_order[999999];                       /* o ... */
int char_index;                               /* p  */

int main(int ch_n_loop, char **dummy)         /* c  */
                                              /* variable with 2 uses */
{

  (void)dummy; /* make warning about unused variable go away */

  while ((ch_n_loop = getchar()) >= 0) /* EOF is, by definition, negative */
  {
    ++char_count[ ( char_order[char_index++] = ch_n_loop ) ];
    /* assignment, and increment, inside the array index */
  }
  /* reuse ch_n_loop */
  for (ch_n_loop = 0; ch_n_loop < char_index; ch_n_loop++) {
    (char_count[char_order[ch_n_loop]] - 1) ? 0 : putchar(char_order[ch_n_loop]);
  }
  return 0;
}
#else
k[256],o[999999],p;main(c){while((c=getchar())>=0)++k[o[p++]=c];for(c=0;c<p;c++)k[o[c]]-1?0:putchar(o[c]);}
#endif

C

(1st version: 112 characters; 2nd version: 107 characters)

k[256],o[100000],p,c;main(){while((c=getchar())!=-1)++k[o[p++]=c];for(c=0;c<p;c++)if(k[o[c]]==1)putchar(o[c]);}

That's

/* #include <stdio.h> */
/* int */ k[256], o[100000], p, c;
/* int */ main(/* void */) {
  while((c=getchar()) != -1/*EOF*/) {
    ++k[o[p++] = /*(unsigned char)*/c];
  }
  for(c=0; c<p; c++) {
    if(k[o[c]] == 1) {
      putchar(o[c]);
    }
  }
  /* return 0; */
}

Because getchar() returns int and putchar accepts int, the #include can 'safely' be removed.
Without the include, EOF is not defined, so I used -1 instead (and gained a char).
This program only works as intended for inputs with less than 100000 characters!

Version 2, with thanks to strager
107 characters

#ifdef NICE_LAYOUT
#include <stdio.h>

/* global variables are initialized to 0 */
int char_count[256];                          /* k in the other layout */
int char_order[999999];                       /* o ... */
int char_index;                               /* p  */

int main(int ch_n_loop, char **dummy)         /* c  */
                                              /* variable with 2 uses */
{

  (void)dummy; /* make warning about unused variable go away */

  while ((ch_n_loop = getchar()) >= 0) /* EOF is, by definition, negative */
  {
    ++char_count[ ( char_order[char_index++] = ch_n_loop ) ];
    /* assignment, and increment, inside the array index */
  }
  /* reuse ch_n_loop */
  for (ch_n_loop = 0; ch_n_loop < char_index; ch_n_loop++) {
    (char_count[char_order[ch_n_loop]] - 1) ? 0 : putchar(char_order[ch_n_loop]);
  }
  return 0;
}
#else
k[256],o[999999],p;main(c){while((c=getchar())>=0)++k[o[p++]=c];for(c=0;c<p;c++)k[o[c]]-1?0:putchar(o[c]);}
#endif
忆悲凉 2024-08-11 00:22:07

Javascript 1.6

s.match(/(.)(?=.*\1)/g).map(function(m){s=s.replace(RegExp(m,'g'),'')})

比之前发布的 Javascript 1.8 解决方案更短(71 个字符与 85 个字符)

Javascript 1.6

s.match(/(.)(?=.*\1)/g).map(function(m){s=s.replace(RegExp(m,'g'),'')})

Shorter than the previously posted Javascript 1.8 solution (71 chars vs 85)

霓裳挽歌倾城醉 2024-08-11 00:22:07

汇编器

使用 WinXP DOS 框 (cmd.exe) 进行测试:

    xchg cx,bp
    std
    mov al,2
    rep stosb
    inc cl
l0: ; to save a byte, I've encoded the instruction to exit the program into the
    ; low byte of the offset in the following instruction:
    lea si,[di+01c3h] 
    push si
l1: mov dx,bp
    mov ah,6
    int 21h
    jz l2
    mov bl,al
    shr byte ptr [di+bx],cl
    jz l1
    inc si
    mov [si],bx
    jmp l1
l2: pop si
l3: inc si
    mov bl,[si]
    cmp bl,bh
    je l0+2
    cmp [di+bx],cl
    jne l3
    mov dl,bl
    mov ah,2
    int 21h
    jmp l3

汇编为 53 字节。读取标准输入并将结果写入标准输出,例如:

 programname < input > output

Assembler

Tested with WinXP DOS box (cmd.exe):

    xchg cx,bp
    std
    mov al,2
    rep stosb
    inc cl
l0: ; to save a byte, I've encoded the instruction to exit the program into the
    ; low byte of the offset in the following instruction:
    lea si,[di+01c3h] 
    push si
l1: mov dx,bp
    mov ah,6
    int 21h
    jz l2
    mov bl,al
    shr byte ptr [di+bx],cl
    jz l1
    inc si
    mov [si],bx
    jmp l1
l2: pop si
l3: inc si
    mov bl,[si]
    cmp bl,bh
    je l0+2
    cmp [di+bx],cl
    jne l3
    mov dl,bl
    mov ah,2
    int 21h
    jmp l3

Assembles to 53 bytes. Reads standard input and writes results to standard output, eg:

 programname < input > output
美煞众生 2024-08-11 00:22:07

PHP

118 个字符的实际代码(加上 PHP 块标记的 6 个字符):

<?php
$s=trim(fgets(STDIN));$x='';while(strlen($s)){$t=str_replace($s[0],'',substr($s,1),$c);$x.=$c?'':$s[0];$s=$t;}echo$x;

PHP

118 characters actual code (plus 6 characters for the PHP block tag):

<?php
$s=trim(fgets(STDIN));$x='';while(strlen($s)){$t=str_replace($s[0],'',substr($s,1),$c);$x.=$c?'':$s[0];$s=$t;}echo$x;
暗藏城府 2024-08-11 00:22:07

C#(53 个字符)

其中 s 是您的输入字符串:

new string(s.Where(c=>s.Count(h=>h==c)<2).ToArray());

或重新赋值后的 59:

var a=new string(s.Where(c=>s.Count(h=>h==c)<2).ToArray());

C# (53 Characters)

Where s is your input string:

new string(s.Where(c=>s.Count(h=>h==c)<2).ToArray());

Or 59 with re-assignment:

var a=new string(s.Where(c=>s.Count(h=>h==c)<2).ToArray());
不喜欢何必死缠烂打 2024-08-11 00:22:07

Haskell Pointfree

import Data.List
import Control.Monad
import Control.Arrow
main=interact$liftM2(\\)nub$ap(\\)nub

整个程序有 97 个字符,但真正的内容只有 23 个字符。剩下的就是导入并将函数带入 IO monad 中。在加载了模块的 ghci 中,它只是

(liftM2(\\)nub$ap(\\)nub) "nbHHkRvrXbvkn"

采用更荒谬的 pointfree 风格(毫无意义的风格?):

main=interact$liftM2 ap liftM2 ap(\\)nub

虽然函数本身有 26 个字符,但它有点长。

Haskell Pointfree

import Data.List
import Control.Monad
import Control.Arrow
main=interact$liftM2(\\)nub$ap(\\)nub

The whole program is 97 characters, but the real meat is just 23 characters. The rest is just imports and bringing the function into the IO monad. In ghci with the modules loaded it's just

(liftM2(\\)nub$ap(\\)nub) "nbHHkRvrXbvkn"

In even more ridiculous pointfree style (pointless style?):

main=interact$liftM2 ap liftM2 ap(\\)nub

It's a bit longer though at 26 chars for the function itself.

枕花眠 2024-08-11 00:22:07

Shell/Coreutils,37 个字符

fold -w1|sort|uniq -u|paste -s -d ''

Shell/Coreutils, 37 Characters

fold -w1|sort|uniq -u|paste -s -d ''
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文