求字符串所有重复两次以上字符
m1rf539w
posted @ 2012年5月29日 08:51
in 数据结构与算法
, 1422 阅读
static void Main(string[] args) { string mete = "aaaassdsfdgfezdxcrgrefsdfdsgdfdfdfsagdfbhdfbtfffgrrssdsdasfcccdghhh"; int s=0; for (int i = 0, l = mete.Length; i < l; i=s) { s= i; int num = RecursionMaxRepate(mete, mete[i], ref s); if (num>1) { Console.WriteLine("{0}:{1}", mete[i], num); } } Console.ReadKey(); } static int RecursionMaxRepate(string m, char c, ref int s) { int sum = 0; if (c == m[s] && (s)<m.Length) { s += 1; if (s >= m.Length) { return sum+1; } sum += RecursionMaxRepate(m, c, ref s) + 1; } else if(c!=m[s]) { return sum; } return sum; }
static void FetchRepate(string s) { for (int i = 0, l = s.Length; i < l; i++) { char c = s[i]; int r = 0; int ii = i + 1; for (; ii < l; ii++) { if (c == s[ii]) { r++; } else { break; } } if (r > 0) { Console.WriteLine("{0}:{1}", c, r+1); i = ii-1; } } }
2012年5月29日 14:08
不明白。直接计数再 filter 不就好了么?
2012年5月31日 10:19
@依云: 嗯,什么意思,我不太明白,还有,我用的是C#.
2012年5月31日 11:55
@m1rf539w: 用 Haskell 的话是这样:(http://p.vim-cn.com/cKw/haskell )
import Control.Monad
import Data.List
import System.Environment (getArgs)
getManyChars = map fst . filter ((>1) . snd) . map getLength . group . sort
where getLength xs@(x:_) = (x, length xs)
main = print . getManyChars . head =<< getArgs
Python 是这样:(http://p.vim-cn.com/cKx/python )
#!/usr/bin/env python3
# vim:fileencoding=utf-8
import sys
from collections import defaultdict
def getManyChars(s):
countMap = defaultdict(int)
for i in s:
countMap[i] += 1
return [c for c, n in countMap.items() if n > 1]
if __name__ == '__main__':
print(getManyChars(sys.argv[1]))
以上代码如果缩进不正确请使用链接来查看。
2012年5月31日 23:53
@依云: python看懂了,但是算法实现,还是不用List,Map之类的封装好的东西了.
2012年6月01日 00:12
@m1rf539w: 哦,原来如此。不过,List 你都不用,那你用什么存放一系列对象呢?
2024年1月16日 17:42
I found your this post while searching for information about blog-related research ... It's a good post .. keep posting and updating information.
2024年2月12日 14:28
I've read some good stuff here. Worth bookmarking for revisit. It's amazing how much effort went into making such a great informational site.
2024年2月12日 14:41
Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except
2024年2月12日 14:48
A decent blog dependably concocts new and energizing data and keeping in mind that understanding I have feel that this blog is truly have each one of those quality that qualify a blog to be a one.
2024年2月12日 14:59
This surely helps me in my work. Lol, thanks for your comment! wink Glad you found it helpful.
2024年2月12日 15:23
Unimaginable Blog! I should need to thank for the endeavors you have made in making this post. I am trusting a practically identical best work from you later on besides. I anticipated that would thank you for this goals! Grateful for sharing.
2024年2月12日 15:31
Very good topic, similar texts are I do not know if they are as good as your work out. These you will then see the most important thing, the application provides you a website a powerful important internet page
2024年2月12日 15:35
I simply want to tell you that I am new to weblog and definitely liked this blog site. Very likely I’m going to bookmark your blog . You absolutely have wonderful stories. Cheers for sharing with us your blog.
2024年2月12日 15:41
Mmm.. awesome to be here in your article or post, whatever, I figure I should moreover lock in for my own particular site like I see some extraordinary and invigorated working in your site.
2024年2月12日 16:01
Its most exceedingly awful piece was that the item just worked intermittently and the data was not exact. You plainly canot go up against anyone about what you have found if the information isn't right.
2024年2月12日 16:09
I was looking through some of your content on this site and I think this website is really informative ! Retain posting.
2024年2月12日 16:18
Many thanks for creating the effort to discuss this, I feel strongly about this and like studying a great deal more on this topic. If feasible, as you gain knowledge, would you mind updating your webpage with a great deal more details? It’s really helpful for me.
2024年2月12日 16:25
There is so much in this article that I would never have thought of on my own. Your content gives readers things to think about in an interesting way. Thank you for your clear information.
2024年2月12日 16:30
Hey, I just hopped over to your site via StumbleUpon. Not somthing I would normally read, but I liked your thoughts none the less. Thanks for making something worth reading.
2024年2月12日 16:45
Hi there! This is kind of off topic but I need some advice from an established blog. Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about setting up my own but I’m not sure where to start.
2024年2月12日 17:03
Just want to say your article is as surprising. The clarity in your post is simply nice and i can assume you’re an expert on this subject. Well with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please continue the rewarding work.
2024年2月12日 17:53
Regular visits listed here are the easiest method to appreciate your energy, which is why why I am going to the website everyday, searching for new, interesting info. Many, thank you
2024年2月12日 18:02
I was taking a gander at some of your posts on this site and I consider this site is truly informational! Keep setting up.
2024年2月12日 18:17
それでもあなたの考えが気に入りました。 読む価値のあるものを作ってくれてありがとう。
2024年2月12日 18:27
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.
2024年2月12日 18:33
It's late discovering this demonstration. At any rate, it's a thing to be acquainted with that there are such occasions exist. I concur with your Blog and I will have returned to examine it more later on so please keep up your demonstration.
2024年2月12日 18:43
I've read some good stuff here. Worth bookmarking for revisit. It's amazing how much effort went into making such a great informational site.
2024年2月12日 18:48
Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except
2024年2月12日 18:55
A decent blog dependably concocts new and energizing data and keeping in mind that understanding I have feel that this blog is truly have each one of those quality that qualify a blog to be a one.
2024年2月12日 19:05
This surely helps me in my work. Lol, thanks for your comment! wink Glad you found it helpful.
2024年2月12日 19:11
Unimaginable Blog! I should need to thank for the endeavors you have made in making this post. I am trusting a practically identical best work from you later on besides. I anticipated that would thank you for this goals! Grateful for sharing.
2024年2月12日 19:20
Very good topic, similar texts are I do not know if they are as good as your work out. These you will then see the most important thing, the application provides you a website a powerful important internet page
2024年2月12日 19:28
I simply want to tell you that I am new to weblog and definitely liked this blog site. Very likely I’m going to bookmark your blog . You absolutely have wonderful stories. Cheers for sharing with us your blog.
2024年2月12日 19:39
Mmm.. awesome to be here in your article or post, whatever, I figure I should moreover lock in for my own particular site like I see some extraordinary and invigorated working in your site.
2024年2月12日 19:47
Its most exceedingly awful piece was that the item just worked intermittently and the data was not exact. You plainly canot go up against anyone about what you have found if the information isn't right.
2024年2月12日 19:53
I was looking through some of your content on this site and I think this website is really informative ! Retain posting.
2024年2月12日 20:03
Many thanks for creating the effort to discuss this, I feel strongly about this and like studying a great deal more on this topic. If feasible, as you gain knowledge, would you mind updating your webpage with a great deal more details? It’s really helpful for me.
2024年2月12日 20:12
There is so much in this article that I would never have thought of on my own. Your content gives readers things to think about in an interesting way. Thank you for your clear information.
2024年2月12日 20:23
Hey, I just hopped over to your site via StumbleUpon. Not somthing I would normally read, but I liked your thoughts none the less. Thanks for making something worth reading.
2024年2月12日 20:30
Hi there! This is kind of off topic but I need some advice from an established blog. Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about setting up my own but I’m not sure where to start.
2024年2月12日 20:39
Just want to say your article is as surprising. The clarity in your post is simply nice and i can assume you’re an expert on this subject. Well with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please continue the rewarding work.