• [CareerCup][Google Interview] 打印组合


    http://www.careercup.com/question?id=3315662

    given a string find the number of distinct substrings of the string.
    ex:
    input-> aaaa
    output-> 4(a, aa, aaa, aaaa)
    input->abcd
    output->10(a, b, c, d, ab, bc, cd, abc, bcd, abcd)

     

    我的方法避免重复是设置一个used数组,记录每个数被使用的情况,如果当前的数和它前一个数相同,则前一个数必须被使用了,当前数才能被使用,这样就能避免了重复出现。

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 
     5 bool used[100];
     6 
     7 void solve(int dep, int maxDep, string &s, string ans, int start)
     8 {
     9     if (dep != 0)
    10         cout << ans << endl;
    11 
    12     if (dep == maxDep)
    13         return;
    14 
    15     for(int i = start; i < s.size(); i++)
    16     {
    17         if (i == 0)
    18         {
    19             used[i] = true;
    20             solve(dep + 1, maxDep, s, ans + s[i], i + 1);
    21             used[i] = false;
    22         }
    23         else
    24         {
    25             if (s[i] == s[i-1])
    26             {
    27                 if (used[i-1])
    28                 {
    29                     used[i] = true;
    30                     solve(dep + 1, maxDep, s, ans + s[i], i + 1);
    31                     used[i] = false;
    32                 }
    33             }
    34             else
    35             {
    36                 used[i] = true;
    37                 solve(dep + 1, maxDep, s, ans + s[i], i + 1);
    38                 used[i] = false;
    39             }
    40         }
    41     }
    42 }
    43 
    44 int main()
    45 {
    46     string a = "aaaa";
    47     memset(used, false, sizeof(used));
    48     solve(0, a.size(), a, "", 0);
    49 
    50     string b = "abcd";
    51     memset(used, false, sizeof(used));
    52     solve(0, a.size(), b, "", 0);
    53 }

     

  • 相关阅读:
    CF263E Rhombus
    AtCoder Grand Contest 034
    Docker C/S 架构逻辑图
    使用filledeat modules配置
    filebeat分别收集多个类型日志
    ELK部署收集日志
    ES界面
    Django下的post请求访问出现403错误
    Django配置(urls.py)
    Day-1 python
  • 原文地址:https://www.cnblogs.com/chkkch/p/2756568.html
Copyright © 2020-2023  润新知