本文转自csdn大神v_JULY_v的博客

地址:

http://blog.csdn.net/v_july_v/article/details/9024123

阅读心得:自己原先想得太天真了。。。

第三十~三十一章:字符串转换成整数,字符串匹配问题

前言

之前本一直想写写神经网络算法和EM算法,但写这两个算法实在需要大段大段的时间,而平时上班,周末则跑去北大教室自习看书(顺便以时间为序,说下过去半年看过的自觉还不错的几本书:《数理统计学简史》《微积分概念发展史》《微积分的历程:从牛顿到勒贝格》《数学恩仇录》《数学与知识的探求》《古今数学思想》《素数之恋》),故一直未曾有时间写。

然最近在负责一款在线编程挑战平台:http://hero.pongo.cn/(简称hero,通俗理解是中国的topcoder,当然,一直在不断完善中,与一般OJ不同点在于,OJ侧重为参与ACM竞赛者提供刷题练习的场所,而hero则着重为企业招聘面试服务),在上面出了几道编程面试题,有些题目看似简单,但一coding,很多问题便立马都在hero上给暴露出来了,故就从hero上的编程挑战题切入,继续更新本程序员编程艺术系列吧。

况且,前几天与一朋友聊天,他说他认识的今年360招进来的三四十人应届生包括他自己找工作时基本都看过我的博客,则更增加了更新此编程艺术系列的动力。

OK,本文讲两个问题:

  • 第三十章、字符串转换成整数;
  • 第三十一章、字符串匹配问题
还是这句老话,有问题恳请随时批评指正,感谢。

第三十章、字符串转换成整数

先看题目:

输入一个表示整数的字符串,把该字符串转换成整数并输出,例如输入字符串"345",则输出整数345。
请完成函数StrToInt,实现字符串转换成整数的功能,不得用库函数atoi。

我们来一步一步分析,直至写出第一份准确的代码:

1、本题考查的实际上就是字符串转换成整数的问题,或者说是要你自行实现atoi函数。那如何实现把表示整数的字符串正确地转换成整数呢?以"345"作为例子:

  1. 当我们扫描到字符串的第一个字符'3'时,由于我们知道这是第一位,所以得到数字3。
  2. 当扫描到第二个数字'4'时,而之前我们知道前面有一个3,所以便在后面加上一个数字4,那前面的3相当于30,因此得到数字:3*10+4=34。
  3. 继续扫描到字符'5','5'的前面已经有了34,由于前面的34相当于340,加上后面扫描到的5,最终得到的数是:34*10+5=345。

因此,此题的思路便是:每扫描到一个字符,我们便把在之前得到的数字乘以10,然后再加上当前字符表示的数字。

2、思路有了,有一些细节需要注意,如zhedahht所说:

  1. “由于整数可能不仅仅之含有数字,还有可能以'+'或者'-'开头,表示整数的正负。因此我们需要把这个字符串的第一个字符做特殊处理。如果第一个字符是'+'号,则不需要做任何操作;如果第一个字符是'-'号,则表明这个整数是个负数,在最后的时候我们要把得到的数值变成负数。
  2. 接着我们试着处理非法输入。由于输入的是指针,在使用指针之前,我们要做的第一件是判断这个指针是不是为空。如果试着去访问空指针,将不可避免地导致程序崩溃。
  3. 另外,输入的字符串中可能含有不是数字的字符。每当碰到这些非法的字符,我们就没有必要再继续转换。
  4. 最后一个需要考虑的问题是溢出问题。由于输入的数字是以字符串的形式输入,因此有可能输入一个很大的数字转换之后会超过能够表示的最大的整数而溢出。”
比如,当给的字符串是如左边图片所示的时候,有考虑到么?当然,它们各自对应的正确输出如右边图片所示(假定你是在32位系统下,且编译环境是VS2008以上):
3、很快,可能你就会写下如下代码:
  1. //copyright@zhedahht 2007
  2. enum Status {kValid = 0, kInvalid};
  3. int g_nStatus = kValid;
  4. // Convert a string into an integer
  5. int StrToInt(const char* str)
  6. {
  7. g_nStatus = kInvalid;
  8. long long num = 0;
  9. if(str != NULL)
  10. {
  11. const char* digit = str;
  12. // the first char in the string maybe '+' or '-'
  13. bool minus = false;
  14. if(*digit == '+')
  15. digit ++;
  16. else if(*digit == '-')
  17. {
  18. digit ++;
  19. minus = true;
  20. }
  21. // the remaining chars in the string
  22. while(*digit != '\0')
  23. {
  24. if(*digit >= '0' && *digit <= '9')
  25. {
  26. num = num * 10 + (*digit - '0');
  27. // overflow
  28. if(num > std::numeric_limits<int>::max())
  29. {
  30. num = 0;
  31. break;
  32. }
  33. digit ++;
  34. }
  35. // if the char is not a digit, invalid input
  36. else
  37. {
  38. num = 0;
  39. break;
  40. }
  41. }
  42. if(*digit == '\0')
  43. {
  44. g_nStatus = kValid;
  45. if(minus)
  46. num = 0 - num;
  47. }
  48. }
  49. return static_cast<int>(num);
  50. }
run下上述程序,会发现当输入字符串是下图中红叉叉部分所对应的时候,程序结果出错:  

两个问题:

  1. 当输入的字符串不是数字,而是字符的时候,比如“1a”,上述程序直接返回了0(而正确的结果应该是得到1):

    1. // if the char is not a digit, invalid input
    2. else
    3. {
    4. num = 0;
    5. break;
    6. }
  2. 处理溢出时,有问题。
4、把代码做下微调,如下:
  1. //copyright@SP_daiyq 2013/5/29
  2. int StrToInt(const char* str)
  3. {
  4. int res = 0; // result
  5. int i = 0; // index of str
  6. int signal = '+'; // signal '+' or '-'
  7. int cur; // current digit
  8. if (!str)
  9. return 0;
  10. // skip backspace
  11. while (isspace(str[i]))
  12. i++;
  13. // skip signal
  14. if (str[i] == '+' || str[i] == '-')
  15. {
  16. signal = str[i];
  17. i++;
  18. }
  19. // get result
  20. while (str[i] >= '0' && str[i] <= '9')
  21. {
  22. cur = str[i] - '0';
  23. // judge overlap or not
  24. if ( (signal == '+') && (cur > INT_MAX - res*10) )
  25. {
  26. res = INT_MAX;
  27. break;
  28. }
  29. else if ( (signal == '-') && (cur -1 > INT_MAX - res*10) )
  30. {
  31. res = INT_MIN;
  32. break;
  33. }
  34. res = res * 10 + cur;
  35. i++;
  36. }
  37. return (signal == '-') ? -res : res;
  38. }
会发现,上面第4小节所述的第1个问题解决了:
但,即使这样,上述代码也还是有问题的。当给定下述测试数据的时候,问题就来了:
需要转换的字符串                         代码运行结果         理应得到的正确结果
"    10522545459" 1932610867 2147483647
"         +10523538441s" 1933603849 2147483647
"   +10432359437" 1842424845 2147483647
什么问题呢?比如说用上述代码转换这个字符串:"    10522545459",它本应得到的正确结果应该是2147483647,但程序实际得到的结果却是:1932610867。故很明显,程序没有很好的解决上面的第2个小问题:溢出问题。

5、上面说给的程序没有“很好的解决溢出问题。由于输入的数字是以字符串的形式输入,因此有可能输入一个很大的数字转换之后会超过能够表示的最大的整数而溢出。”那么,到底代码该如何写呢?

  1. //copyright@淹死鲨鱼ronkins 2013/5/29
  2. //挑战题目:http://hero.pongo.cn/Question/Details?ID=47&ExamID=45
  3. int atoi(const char* str)
  4. {
  5. long long res = 0;
  6. int sign = 1;
  7. while(isspace(*str))++str;
  8. if('+' == *str){
  9. ++str;
  10. }else if('-' == *str){
  11. sign = -1;
  12. ++str;
  13. }
  14. for(; isdigit(*str); ++str){
  15. res *= 10;
  16. if(sign > 0)
  17. res += (*str - '0');
  18. else
  19. res -= (*str - '0');
  20. if(res >= INT_MAX)return INT_MAX;
  21. else if(res <= INT_MIN)return INT_MIN;
  22. }
  23. return res;
  24. }

上面的代码看似能处理数据溢出的问题,其实它只是做了个取巧,即把返回的值res定义成了long long,如下所示:

  1. long long res = 0;

故严格说来,我们依然未写出准确的规范代码。

6、那到底该如何解决这个数据溢出的问题呢?库函数atoi的规定超过int值,按最大值maxint:2147483647来,超过-int按最小值minint:-2147483648来。咱们先来看看Microsoft是如何实现atoi的吧:
  1. //atol函数
  2. //Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  3. long __cdecl atol(
  4. const char *nptr
  5. )
  6. {
  7. int c; /* current char */
  8. long total; /* current total */
  9. int sign; /* if ''-'', then negative, otherwise positive */
  10. /* skip whitespace */
  11. while ( isspace((int)(unsigned char)*nptr) )
  12. ++nptr;
  13. c = (int)(unsigned char)*nptr++;
  14. sign = c; /* save sign indication */
  15. if (c == ''-'' || c == ''+'')
  16. c = (int)(unsigned char)*nptr++; /* skip sign */
  17. total = 0;
  18. while (isdigit(c)) {
  19. total = 10 * total + (c - ''0''); /* accumulate digit */
  20. c = (int)(unsigned char)*nptr++; /* get next char */
  21. }
  22. if (sign == ''-'')
  23. return -total;
  24. else
  25. return total; /* return result, negated if necessary */
  26. }
其中,isspace和isdigit函数的实现代码为:
  1. isspace(int x)
  2. {
  3. if(x==' '||x=='/t'||x=='/n'||x=='/f'||x=='/b'||x=='/r')
  4. return 1;
  5. else
  6. return 0;
  7. }
  8. isdigit(int x)
  9. {
  10. if(x<='9'&&x>='0')
  11. return 1;
  12. else
  13. return 0;
  14. }
然后atoi调用上面的atol函数,如下所示:
  1. //atoi调用上述的atol
  2. int __cdecl atoi(
  3. const char *nptr
  4. )
  5. {
  6. //Overflow is not detected. Because of this, we can just use
  7. return (int)atol(nptr);
  8. }

但很遗憾的是,上述atoi标准代码依然返回的是long:

  1. long total; /* current total */
  2. if (sign == ''-'')
  3. return -total;
  4. else
  5. return total; /* return result, negated if necessary */

再者,下面这里定义成long的total与10相乘,即total*10很容易溢出:

  1. long total; /* current total */
  2. total = 10 * total + (c - ''0''); /* accumulate digit */
     7、继续寻找。接下来,咱们来看看linux内核中是如何实现此字符串转换为整数的问题的。
linux内核中提供了以下几个函数:
  1. simple_strtol,把一个字符串转换为一个有符号长整数;
  2. simple_strtoll,把一个字符串转换为一个有符号长长整数;
  3. simple_strtoul,把一个字符串转换为一个无符号长整数;
  4. simple_strtoull,把一个字符串转换为一个无符号长长整数
相关源码及分析如下。
首先,atoi调下面的strtol:
  1. //linux/lib/vsprintf.c
  2. //Copyright (C) 1991, 1992  Linus Torvalds
  3. //simple_strtol - convert a string to a signed long
  4. long simple_strtol(const char *cp, char **endp, unsigned int base)
  5. {
  6. if (*cp == '-')
  7. return -simple_strtoul(cp + 1, endp, base);
  8. return simple_strtoul(cp, endp, base);
  9. }
  10. EXPORT_SYMBOL(simple_strtol);

然后,上面的strtol调下面的strtoul:

  1. //simple_strtoul - convert a string to an unsigned long
  2. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
  3. {
  4. return simple_strtoull(cp, endp, base);
  5. }
  6. EXPORT_SYMBOL(simple_strtoul);

接着,上面的strtoul调下面的strtoull:

  1. //simple_strtoll - convert a string to a signed long long
  2. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  3. {
  4. if (*cp == '-')
  5. return -simple_strtoull(cp + 1, endp, base);
  6. return simple_strtoull(cp, endp, base);
  7. }
  8. EXPORT_SYMBOL(simple_strtoll);

最后,strtoull调_parse_integer_fixup_radix和_parse_integer来处理相关逻辑:

  1. //simple_strtoull - convert a string to an unsigned long long
  2. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  3. {
  4. unsigned long long result;
  5. unsigned int rv;
  6. cp = _parse_integer_fixup_radix(cp, &base);
  7. rv = _parse_integer(cp, base, &result);
  8. /* FIXME */
  9. cp += (rv & ~KSTRTOX_OVERFLOW);
  10. if (endp)
  11. *endp = (char *)cp;
  12. return result;
  13. }
  14. EXPORT_SYMBOL(simple_strtoull);

重头戏来了。接下来,我们来看上面strtoull函数中的parse_integer_fixup_radix和_parse_integer两段代码。如鲨鱼所说

  • “真正的处理逻辑主要是在_parse_integer里面,关于溢出的处理,_parse_integer处理的很优美,
  • 而_parse_integer_fixup_radix是用来自动根据字符串判断进制的”。
先来看_parse_integer函数:

  1. //lib/kstrtox.c, line 39
  2. //Convert non-negative integer string representation in explicitly given radix to an integer.
  3. //Return number of characters consumed maybe or-ed with overflow bit.
  4. //If overflow occurs, result integer (incorrect) is still returned.
  5. unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
  6. {
  7. unsigned long long res;
  8. unsigned int rv;
  9. int overflow;
  10. res = 0;
  11. rv = 0;
  12. overflow = 0;
  13. while (*s) {
  14. unsigned int val;
  15. if ('0' <= *s && *s <= '9')
  16. val = *s - '0';
  17. else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f')
  18. val = _tolower(*s) - 'a' + 10;
  19. else
  20. break;
  21. if (val >= base)
  22. break;
  23. /*
  24. * Check for overflow only if we are within range of
  25. * it in the max base we support (16)
  26. */
  27. if (unlikely(res & (~0ull << 60))) {
  28. if (res > div_u64(ULLONG_MAX - val, base))
  29. overflow = 1;
  30. }
  31. res = res * base + val;
  32. rv++;
  33. s++;
  34. }
  35. *p = res;
  36. if (overflow)
  37. rv |= KSTRTOX_OVERFLOW;
  38. return rv;
  39. }
解释下两个小细节:
  1. 上头出现了个unlikely,其实unlikely和likely经常出现在linux相关内核源码中

    1. if(likely(value)){
    2. //等价于if(likely(value)) == if(value)
    3. }
    4. else{
    5. }

    likely表示value为真的可能性更大,而unlikely表示value为假的可能性更大,这两个宏被定义成:

    1. //include/linux/compiler.h
    2. # ifndef likely
    3. #  define likely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
    4. # endif
    5. # ifndef unlikely
    6. #  define unlikely(x)   (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
    7. # endif
  2. 呈现下div_u64的代码:
    1. //include/linux/math64.h
    2. //div_u64
    3. static inline u64 div_u64(u64 dividend, u32 divisor)
    4. {
    5. u32 remainder;
    6. return div_u64_rem(dividend, divisor, &remainder);
    7. }
    8. //div_u64_rem
    9. static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
    10. {
    11. *remainder = dividend % divisor;
    12. return dividend / divisor;
    13. }
最后看下_parse_integer_fixup_radix函数:

  1. //lib/kstrtox.c, line 23
  2. const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
  3. {
  4. if (*base == 0) {
  5. if (s[0] == '0') {
  6. if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
  7. *base = 16;
  8. else
  9. *base = 8;
  10. } else
  11. *base = 10;
  12. }
  13. if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
  14. s += 2;
  15. return s;
  16. }
OK,至此,字符串转换成整数的问题算是已经解决。如果面试官继续问你,如何把整数转换成字符串呢?请读者思考,同时也欢迎于本文评论下或hero上show your code。

第三十一章、字符串匹配问题

字符串匹配问题,给定一串字符串,按照指定规则对其进行匹配,并将匹配的结果保存至output数组中,多个匹配项用空格间隔,最后一个不需要空格。

要求:

  1. 匹配规则中包含通配符?和*,其中?表示匹配任意一个字符,*表示匹配任意多个(>=0)字符。
  2. 匹配规则要求匹配最大的字符子串,例如a*d,匹配abbdd而非abbd,即最大匹配子串。
  3. 匹配后的输入串不再进行匹配,从当前匹配后的字符串重新匹配其他字符串。

请实现函数:char* my_find(char  input[],   char rule[])

举例说明

input:abcadefg
rule:a?c
output:abc

input :newsadfanewfdadsf
rule: new
output: new new

input :breakfastfood
rule: f*d
output:fastfood

注意事项:

  1. 自行实现函数my_find,勿在my_find函数里夹杂输出,且不准用C、C++库,和Java的String对象;
  2. 请注意代码的时间,空间复杂度,及可读性,简洁性;
  3. input=aaa,rule=aa时,返回一个结果aa,即可。

1、本题与上述第三十章的题不同,上题字符串转换成整数更多考察对思维的全面性和对细节的处理,本题则更多的是编程技巧。闲不多说,直接上代码:

  1. //copyright@cao_peng 2013/4/23
  2. int str_len(char *a) {  //字符串长度
  3. if (a == 0) {
  4. return 0;
  5. }
  6. char *t = a;
  7. for (;*t;++t)
  8. ;
  9. return (int) (t - a);
  10. }
  11. void str_copy(char *a,const char *b,int len) {  //拷贝字符串 a = b
  12. for (;len > 0; --len, ++b,++a) {
  13. *a = *b;
  14. }
  15. *a = 0;
  16. }
  17. char *str_join(char *a,const char *b,int lenb) { //连接字符串 第一个字符串被回收
  18. char *t;
  19. if (a == 0) {
  20. t = (char *) malloc(sizeof(char) * (lenb + 1));
  21. str_copy(t, b, lenb);
  22. return t;
  23. }
  24. else {
  25. int lena = str_len(a);
  26. t = (char *) malloc(sizeof(char) * (lena + lenb + 2));
  27. str_copy(t, a, lena);
  28. *(t + lena) = ' ';
  29. str_copy(t + lena + 1, b, lenb);
  30. free(a);
  31. return t;
  32. }
  33. }
  34. int canMatch(char *input, char *rule) { // 返回最长匹配长度 -1表示不匹配 
  35. if (*rule == 0) { //已经到rule尾端
  36. return 0;
  37. }
  38. int r = -1 ,may;
  39. if (*rule == '*') {
  40. r = canMatch(input, rule + 1);  // *匹配0个字符
  41. if (*input) {
  42. may = canMatch(input + 1, rule);  // *匹配非0个字符
  43. if ((may >= 0) && (++may > r)) {
  44. r = may;
  45. }
  46. }
  47. }
  48. if (*input == 0) {  //到尾端
  49. return r;
  50. }
  51. if ((*rule == '?') || (*rule == *input)) {
  52. may = canMatch(input + 1, rule + 1);
  53. if ((may >= 0) && (++may > r)) {
  54. r = may;
  55. }
  56. }
  57. return r;
  58. }
  59. char * my_find(char  input[],   char rule[]) {
  60. int len = str_len(input);
  61. int *match = (int *) malloc(sizeof(int) * len);  //input第i位最多能匹配多少位 匹配不上是-1
  62. int i,max_pos = - 1;
  63. char *output = 0;
  64. for (i = 0; i < len; ++i) {
  65. match[i] = canMatch(input + i, rule);
  66. if ((max_pos < 0) || (match[i] > match[max_pos])) {
  67. max_pos = i;
  68. }
  69. }
  70. if ((max_pos < 0) || (match[max_pos] <= 0)) {  //不匹配
  71. output = (char *) malloc(sizeof(char));
  72. *output = 0;   // \0
  73. return output;
  74. }
  75. for (i = 0; i < len;) {
  76. if (match[i] == match[max_pos]) { //找到匹配
  77. output = str_join(output, input + i, match[i]);
  78. i += match[i];
  79. }
  80. else {
  81. ++i;
  82. }
  83. }
  84. free(match);
  85. return output;
  86. }

 2、本题也可以直接写出DP方程,如下代码所示:

  1. //copyright@chpeih 2013/4/23
  2. char * my_find(char  input[],   char rule[]) {
  3. int len = str_len(input);
  4. int *match = (int *) malloc(sizeof(int) * len);  //input第i位最多能匹配多少位 匹配不上是-1
  5. int i,max_pos = - 1;
  6. char *output = 0;
  7. for (i = 0; i < len; ++i) {
  8. match[i] = canMatch(input + i, rule);
  9. if ((max_pos < 0) || (match[i] > match[max_pos])) {
  10. max_pos = i;
  11. }
  12. }
  13. if ((max_pos < 0) || (match[max_pos] <= 0)) {  //不匹配
  14. output = (char *) malloc(sizeof(char));
  15. *output = 0;   // \0
  16. return output;
  17. }
  18. for (i = 0; i < len;) {
  19. if (match[i] == match[max_pos]) { //找到匹配
  20. output = str_join(output, input + i, match[i]);
  21. i += match[i];
  22. }
  23. else {
  24. ++i;
  25. }
  26. }
  27. free(match);
  28. return output;
  29. }
  30. char* my_find(char  input[],   char rule[])
  31. {
  32. //write your code here
  33. int len1,len2;
  34. for(len1 = 0;input[len1];len1++);
  35. for(len2 = 0;rule[len2];len2++);
  36. int MAXN = len1>len2?(len1+1):(len2+1);
  37. int  **dp;
  38. //dp[i][j]表示字符串1和字符串2分别以i j结尾匹配的最大长度
  39. //记录dp[i][j]是由之前那个节点推算过来  i*MAXN+j
  40. dp = new int *[len1+1];
  41. for (int i = 0;i<=len1;i++)
  42. {
  43. dp[i] = new int[len2+1];
  44. }
  45. dp[0][0] = 0;
  46. for(int i = 1;i<=len2;i++)dp[0][i] = -1;
  47. for(int i = 1;i<=len1;i++)dp[i][0] = 0;
  48. for (int i = 1;i<=len1;i++)
  49. {
  50. for (int j = 1;j<=len2;j++)
  51. {
  52. if(rule[j-1]=='*'){
  53. dp[i][j] = -1;
  54. if (dp[i-1][j-1]!=-1)
  55. {
  56. dp[i][j] = dp[i-1][j-1]+1;
  57. }
  58. if (dp[i-1][j]!=-1 && dp[i][j]<dp[i-1][j]+1)
  59. {
  60. dp[i][j] = dp[i-1][j]+1;
  61. }
  62. }else if (rule[j-1]=='?')
  63. {
  64. if(dp[i-1][j-1]!=-1){
  65. dp[i][j] = dp[i-1][j-1]+1;
  66. }else dp[i][j] = -1;
  67. }
  68. else
  69. {
  70. if(dp[i-1][j-1]!=-1 && input[i-1]==rule[j-1]){
  71. dp[i][j] = dp[i-1][j-1]+1;
  72. }else dp[i][j] = -1;
  73. }
  74. }
  75. }
  76. int m = -1;//记录最大字符串长度
  77. int *ans = new int[len1];
  78. int count_ans = 0;//记录答案个数
  79. char *returnans = new char[len1+1];
  80. int count = 0;
  81. for(int i = 1;i<=len1;i++)
  82. if (dp[i][len2]>m){
  83. m = dp[i][len2];
  84. count_ans = 0;
  85. ans[count_ans++] = i-m;
  86. }else if(dp[i][len2]!=-1 &&dp[i][len2]==m){
  87. ans[count_ans++] = i-m;
  88. }
  89. if (count_ans!=0)
  90. {
  91. int len = ans[0];
  92. for (int i = 0;i<m;i++)
  93. {
  94. printf("%c",input[i+ans[0]]);
  95. returnans[count++] = input[i+ans[0]];
  96. }
  97. for (int j = 1;j<count_ans;j++)
  98. {
  99. printf(" ");
  100. returnans[count++] = ' ';
  101. len = ans[j];
  102. for (int i = 0;i<m;i++)
  103. {
  104. printf("%c",input[i+ans[j]]);
  105. returnans[count++] = input[i+ans[j]];
  106. }
  107. }
  108. printf("\n");
  109. returnans[count++] = '\0';
  110. }
  111. return returnans;
  112. }

欢迎于本文评论下或hero上 show your code。

参考文献及推荐阅读

  1. http://zhedahht.blog.163.com/blog/static/25411174200731139971/;
  2. http://hero.pongo.cn/,本文大部分代码都取自左边hero上参与答题者提交的代码,欢迎你也去挑战;
  3. 字符串转换成整数题目完整描述:http://hero.pongo.cn/Question/Details?ID=47&ExamID=45;
  4. 字符串匹配问题题目完整描述:http://hero.pongo.cn/Question/Details?ID=28&ExamID=28;
  5. linux3.8.4版本下的相关字符串整数转换函数概览:https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/lib/vsprintf.c?id=refs/tags/v3.9.4;
  6. 关于linux中的likely和unlikely:http://blog.21ic.com/user1/5593/archives/2010/68193.html;

字符串转换成整数,字符串匹配问题相关推荐

  1. 程序员编程艺术第三十 三十一章 字符串转换成整数,通配符字符串匹配

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 第三十~ ...

  2. 字符串转换成整数,带通配符的字符串匹配

    之前本一直想写写神经网络算法和EM算法,但写这两个算法实在需要大段大段的时间,而平时上班,周末则跑去北大教室自习看书(顺便以时间为序,说下过去半年看过的自觉还不错的数学史方面的书:<数理统计学简 ...

  3. 程序员编程艺术第三十~三十一章:字符串转换成整数,通配符字符串匹配

    第三十~三十一章:字符串转换成整数,带通配符的字符串匹配 前言 之前本一直想写写神经网络算法和EM算法,但写这两个算法实在需要大段大段的时间,而平时上班,周末则跑去北大教室自习看书(顺便以时间为序,说 ...

  4. 字符串转换成整数,通配符的字符串匹配问题

    http://blog.csdn.net/v_july_v/article/details/9024123#comments 前言 之前本一直想写写神经网络算法和EM算法,但写这两个算法实在需要大段大 ...

  5. 判断字符为空_49. 把字符串转换成整数(剑指offer)

    49. 把字符串转换成整数 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一个合法的数值则返回0. 输入描述: 输入一个字符串,包括数字字母符号,可以为空 输 ...

  6. c语言字符怎么变成整数,c语言,字符串转换成整数

    c语言的数字字符串转换为整数,1.可接受"123 2123"处理为1232123;2.空指针.正负号.非纯数字字符串.数据越界溢出的错误处理. #include #include ...

  7. 如何把一个字符串转换成整数

    剑指offer第一章的例子,据说是微软的面试题,发现自己又躺枪了.字符串处理有多烦人不用我多说了吧. //基础版代码 int StrToInt(char* string) {int number = ...

  8. oracle 转化为整数,字符串转换成整数——从源码学习

    字符串转换成整数:输入一个表示整数的字符串,把该字符串转换成整数并输出,例如输入字符串"345",则输出整数345. 在笔试面试中,atoi 即「字符串转换成整数」是一个经典问题了 ...

  9. python 字符串转换成整数

    | String to Int 写一个函数 StrToInt,实现把字符串转换成整数这个功能.不能使用 atoi 或者其他类似的库函数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个 ...

最新文章

  1. c语言后缀表达式构造二叉树,C ++程序为后缀表达式构造表达式树
  2. [AngularJS学习笔记] 基础学习01
  3. LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)
  4. 励志:读书七年,为了挣钱,我竭尽全力
  5. 03程序结构if for while
  6. Codeforces Round #716 (Div. 2) D. Cut and Stick 主席树 + 思维
  7. “Device eth0 does not seem to be present”解决办法
  8. 不要在有反馈的平台上输出
  9. 做游戏,学编程(C语言) 1 实现弹跳小球
  10. Robo 可视化mongoDb的操作
  11. Linux应用开发【第十四章】CAN编程应用开发
  12. 第二十八篇 -- 学习第五十一天打卡20190819
  13. 【华为云技术分享】漫谈LIteOS-物联网操作系统介绍
  14. 执行Hive SQL时报错:Map operator initialization failed
  15. 深度学习配置环境全攻略
  16. 手游平台开发怎么做?
  17. 计算机一级wps选择题必背知识点,计算机一级WPS提高练习题及答案
  18. 数字电视至显示android,手机投屏到电视的5种方法 看完才知道原来这么简单!
  19. 你见过哪些令你瞠目结舌的Python代码技巧?
  20. MyEclipse 2013优化技巧

热门文章

  1. 【conda安装pytorch总是下载cpu版本的问题】
  2. 数据结构python-第三节
  3. 易查分怎么上传成绩?学会这个技巧,轻松搞定
  4. c++ templete
  5. 【如何让图片自适应盒子大小】
  6. java实现裁剪算法代码_Cyrus-Beck图像裁剪算法归纳
  7. 每日一题198—划分字母区间
  8. Azure Kinect DK入门学习(一)设置Azure Kinect DK
  9. 《Python编程:从入门到实践》练习13-1、13-2
  10. 数学之美 - Python视角下的Peter de Jong吸引子