计算用户输入的双倍或重复字符的功能 [英] Function that counts doubles or duplicate characters entered by user

查看:100
本文介绍了计算用户输入的双倍或重复字符的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对用户输入的字符加倍数进行计数的函数

function that counts the number of character doubles entered by the user

推荐答案

另一种方法是对字符串中的字符进行排序,然后检查排序后的字符串. br/>
重复的字符很容易找到,因为它们彼此相邻.
Another method is to sort the characters in the string, then examine the sorted string.

Duplicate characters will be easy to find, since they will be next to each other.


解决方案1是您应跟踪每个字符出现的次数:

Solution 1 is that you should keep track of how many times each character appears:

int count[256]; // <-- assuming char is 8 bytes
for(i=0;i!=256;++i)
{
    count[i] = 0; // <-- set all counts to be zero
}
for(i=0;arr[i]!='\0';i++)
{
    count[arr[i]] = count[arr[i]] + 1;
    // now you can check if count is 1, and if so then do whatever
}


首先要回答两个问题:

-记忆或执行时间哪个更重要?
-您要处理的字符集有多大?

如果与字符集大小的大小相比,您有足够的可用内存,那么可能需要一个完整的频率表(如解决方案一).请使用C ++,而不要使用C遗物[1].使用:
There are two questions to answer first:

- Which is more important, memory or execution time?
- How big is the character set you''re processing?

If you''ve got plenty of memory available compared to the size of the size of the character set then a full frequency table (as in solution one) is probably the way to go. Please use C++ though and not some C relic[1]. Use:
std::vector<int> frequency_table( 256 );

而不是数组.您甚至可以快闪,并使用std::for_each在流或另一个集合中的很多行中进行计数.

如果受内存限制,或者您无法在应用程序的工作集中容纳完整的频率表,则可以使用稀疏频率表.制作一个整数到整数的映射:

rather than an array. You could even be flash and use std::for_each to do the counting in not many lines from a stream or another collection.

If you''re limited by memory OR you can''t fit the full frequency table in the application''s working set then you can use a sparse frequency table. Make a map of ints to ints:

std::map<int,int> frequency_table;

,并像其他解决方案中的表格一样精确地使用它.它会变慢,但是如果这是代码正常工作与不正常工作之间的区别,那么我每天都会变慢.

而且,如果将所有内容都埋在一个类中,则可以在正在使用的集合上创建很多模板,如果太慢/太大,则可以对其进行更改.什么不喜欢?

哦,最后一件事.如果您的代码旨在处理UTF-8或UTF-16,请记住,在这些表示形式中,一个C ++ char/wchar_t可能不是完整字符.

[1]我用遗物"一词指的是在C ++程序中使用C构造的实践.我不是说C是一个遗物,而是-仍然是地球上最重要的语言之一.

为编辑器编辑失败-我应该更加小心粘贴内容

and use it exactly as you would the table in the other solution. It''ll be slower but if it''s the difference between code working and not working I''ll take slower any day.

And if you bury it all in a class you can template the lot on the collection you''re using and change it if it''s too slow/too big. What''s not to like?

Oh, one final thing. If your code is meant to handle UTF-8 or UTF-16 remember that one C++ char/wchar_t may not be a full character in those representations.

[1] I use the word relic to refer to the practice of using C constructs in C++ programs. I''m not calling C a relic, it''s not - it''s still one of the most important languages on the planet.

Edit for editor fail - I should be more careful about pasting stuff


这篇关于计算用户输入的双倍或重复字符的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆