惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Threatpost
NISL@THU
NISL@THU
A
Arctic Wolf
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Tenable Blog
O
OpenAI News
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
Cloudbric
Cloudbric
PCI Perspectives
PCI Perspectives
爱范儿
爱范儿
GbyAI
GbyAI
U
Unit 42
IT之家
IT之家
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
Last Week in AI
Last Week in AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
AWS News Blog
AWS News Blog
Schneier on Security
Schneier on Security
L
LINUX DO - 最新话题
Latest news
Latest news
Hacker News: Ask HN
Hacker News: Ask HN
W
WeLiveSecurity
TaoSecurity Blog
TaoSecurity Blog
Attack and Defense Labs
Attack and Defense Labs
Scott Helme
Scott Helme
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Securelist
Help Net Security
Help Net Security
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX
S
Security @ Cisco Blogs
月光博客
月光博客
P
Proofpoint News Feed
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog
博客园 - 叶小钗
C
Check Point Blog
腾讯CDC
The Cloudflare Blog
Simon Willison's Weblog
Simon Willison's Weblog

博客园 - 亦心

教你如何查找并下载某手某音上的外部视频 年会抽奖软件 正则表达式+编码转换小工具 sql语句跨库导入导出 Google maps API开发(二) - 亦心 Google maps API开发(一) - 亦心 Javascript树型菜单(含源码) Javascript结合XML省市级联 - 亦心 - 博客园 ASP.NET程序读取二代身份证(附源码) - 亦心 - 博客园 仿百度搜索智能提示(纯JS实现) 发现一个免费申请国际域名的地方!! asp.net性能优化 分享一个分页存储过程和分页函数 利用反射实现通用的DataReader转List、DataReader转实体类 任意类型转换成json tsql字符串操作 javascript正则表达式 - 亦心 - 博客园 access、excel取随机n条记录 测试SQL Server执行时间和CPU时间
求出两个字符串中最大长度的相同的子字符串
亦心 · 2014-05-14 · via 博客园 - 亦心
  1    class Program
  2     {
  3         static void Main(string[] args)
  4         {
  5             string s1 = "abcdefghijklmn";
  6             string s2 = "abc";
  7             string s3 = "bcde";
  8             string s4 = "lmn";
  9             string s5 = "abdef";
 10             string s6 = "bcabcd";
 11             string s7 = "abcdghabcdef";
 12             string s8 = "events/eventinsight/insight";
 13             string s9 = "events";
 14             Console.WriteLine("s1:{0},s1:{1} {2} ", s1, s1, GetMaxSameString(s1, s1));
 15             Console.WriteLine("s1:{0},s2:{1} {2} ", s1, s2, GetMaxSameString(s1, s2));
 16             Console.WriteLine("s2:{0},s3:{1} {2} ", s2, s3, GetMaxSameString(s2, s3));
 17             Console.WriteLine("s3:{0},s4:{1} {2} ", s3, s4, GetMaxSameString(s3, s4));
 18             Console.WriteLine("s4:{0},s5:{1} {2} ", s4, s5, GetMaxSameString(s4, s5));
 19             Console.WriteLine("s1:{0},s6:{1} {2} ", s1, s6, GetMaxSameString(s1, s6));
 20             Console.WriteLine("s1:{0},s7:{1} {2} ", s1, s7, GetMaxSameString(s1, s7));
 21             Console.WriteLine("s8:{0},s9:{1} {2} ", s8, s9, GetMaxSameString(s8, s9));
 22             Console.ReadLine();
 23         }
 24         /// <summary>
 25         /// 得到某一字符在另一字符串中的index数组
 26         /// </summary>
 27         /// <param name="chr">某一字符</param>
 28         /// <param name="str">另一字符串</param>
 29         /// <returns>index数组</returns>
 30         static List<int> GetIndexArr(char chr, string str)
 31         {
 32             List<int> arr = new List<int>();
 33             for (int x = 0, len = str.Length; x < len; x++)
 34             {
 35                 if (chr.Equals(str[x]))
 36                 {
 37                     arr.Add(x);
 38                 }
 39             }
 40             return arr;
 41         }
 42 
 43         /// <summary>
 44         /// 得到某一字符串数组中的最长字符
 45         /// </summary>
 46         /// <param name="list">某一字符串数组</param>
 47         /// <returns>最长字符</returns>
 48         static string GetMaxLenString(List<string> list)
 49         {
 50             string str = "";
 51             if (list.Count > 0)
 52             {
 53                 str = list[0];
 54                 for (int k = 0; k < list.Count; k++)
 55                 {
 56                     if (list[k].Length > str.Length)
 57                     {
 58                         str = list[k];
 59                     }
 60                 }
 61             }
 62             return str;
 63         }
 64 
 65         /// <summary>
 66         /// 得到最大相同长度字符串
 67         /// </summary>
 68         /// <param name="s1">字符串1</param>
 69         /// <param name="s2">字符串2</param>
 70         /// <returns>最大相同长度字符串</returns>
 71         static string GetMaxSameString(string s1, string s2)
 72         {
 73             List<string> list = new List<string>();
 74             int l1, l2;
 75             string maxStr,minStr;
 76             if (s1.Length > s2.Length)
 77             {
 78                 l1 = s1.Length;
 79                 l2 = s2.Length;
 80                 minStr = s2;
 81                 maxStr = s1;
 82             }
 83             else
 84             {
 85                 l1 = s2.Length;
 86                 l2 = s1.Length;
 87                 minStr = s1;
 88                 maxStr = s2;
 89             }
 90 
 91             for (int i = 0; i < l1; i++)
 92             {
 93                 int j = 0;
 94                 string sb = "";
 95                 char chr = maxStr[i];
 96                 List<int> arr = GetIndexArr(chr, s2);
 97                 for (int y = 0; y < arr.Count; y++)
 98                 {
 99                     j = arr[y];
100                     int k = i;
101                     while (k < l1 && j < l2)
102                     {
103                         if (maxStr[k] == minStr[j])
104                         {
105                             sb += minStr[j];
106                             k++;
107                         }
108                         j++;
109                     }
110                     if (sb.Length > 0)
111                     {
112                         list.Add(sb);
113                         sb = "";
114                     }
115                 }
116             }
117             return GetMaxLenString(list);
118         }
119     }