zoukankan      html  css  js  c++  java
  • 字符串全组合(淘宝宝贝关键字)

      小伙伴最近开了家淘宝店,每天为宝贝的名称弄得很麻烦,经常需要根据不同的关键字来组合宝贝的名称,但不知道啥组合能引入更多的流量,所以每个都试着弄弄看,所以这就需要一个字符串的组合。

      在那个夜黑风高的晚上,小伙伴带着我去华强北吃了寿司,无意间谈了这个小东西,实现难度不高,立刻动手:

     1 internal class Program
     2     {
     3         /*
     4          思路:获取需要组合的字符串数组,循环读取字符串数组成立组合,再根据该组合循环再去组合,其实就是递归读取,用Contains判断是有包含即可
     5          */
     6         private static void Main(String[] args)
     7         {
     8             //调用方法,传入字符串
     9             showStr();
    10             Console.Read();
    11         }
    12 
    13         public static void showStr()
    14         {
    15             string[] str1 = new string[] {"好用的", "4s", "苹果", "乔布斯"}; //原始数组
    16             string[] str2 = new string[] {"好用的", "4s", "苹果", "乔布斯"}; //变化数组
    17 
    18             //循环读取str1字符串数组
    19             for (int x = 2; x <= str1.Length; x++)
    20             {
    21                 //调用方法,传入 原始字符串数组 和 前一次的字符串数组,获得新的字符串数组
    22                 str2 = ChangeStr(str1, str2);
    23                 if (str2 == null)
    24                 {
    25                     break;
    26                 }
    27                 PrintString(str2); //输出本次的转换
    28             }
    29         }
    30 
    31 
    32         //定义方法,接收 原始字符串数组 和 前一次的字符串数组,并返回新字符串数组
    33         public static String[] ChangeStr(String[] chs, String[] oldStrs)
    34         {
    35             //因为新字符串数组长度暂时无法确定,也为了转换方便,所以使用StringBuilder来接收每个新字符串
    36             StringBuilder sb = new StringBuilder();
    37             //定义新字符串数组                
    38             String[] newStrs = null;
    39 
    40             //外循环,遍历每个字符串数组
    41             for (int x = 0; x < oldStrs.Length; x++)
    42             {
    43                 //内循环,遍历每个原始字符串,将每个字符串把每个不同字符都单独添加一次
    44                 for (int y = 0; y < chs.Length; y++)
    45                 {
    46                     //判断字符串是否包含该字符串
    47                     if (oldStrs[x].Contains(chs[y]))
    48                         //已包含该字符,则不操作,继续下次循环
    49                         continue;
    50                     //不包含该字符,则添加该字符
    51                     String s = oldStrs[x] + chs[y];
    52                     //添加新字符串到StringBuilder对象并用','隔开
    53                     sb.Append(s).Append(',');
    54                 }
    55             }
    56             //获得新字符串数组
    57             if (sb.Length != 0)
    58             {
    59                 newStrs = sb.Remove(sb.Length - 1, 1).ToString().Split(',');
    60             }
    61             return newStrs;
    62         }
    63 
    64         public static void PrintString(String[] strs)
    65         {
    66             foreach (var str in strs)
    67             {
    68                 Console.Write(str);
    69                 Console.WriteLine();
    70             }
    71         }
    72     }
    View Code

      这个也是软件的算法之一,其实这个可以衍生更多好玩的,例如记录这个关键字组合用了多少次,引入了多少流量,实现这功能用什么交互等,这也是下一步需要做的~

  • 相关阅读:
    初识Node
    从服务器获取信息的方式
    引用对象的指针传递
    使用定时器来控制一次执行的任务数量
    JS字符串拼接的方法及性能比较
    提升JS比较效率的方式
    DOM访问优化
    JS数据存取效率的优化
    JS脚本加载顺序及优化
    python 基本用法
  • 原文地址:https://www.cnblogs.com/powerdk/p/4484153.html
Copyright © 2011-2022 走看看