一个字符串的操作可以求子串并定位

            随便写的一个字符串的操作可以在一个子串中求出某一个子串的出现次数和出现的位置。

 

#include < iostream >
#include
< string >
using   namespace  std;

int   PartPostion( char  src[],  char  partString[],  int  pos) 
                                                                                                 
// 从某一个位置开始定位子串的函数
{
    
int i = pos;                                                                          //从pos位置开始
    int j = 0;
    
int length_src = strlen(src);
    
int lenght_partString = strlen(partString);
    
while(i < length_src && j < lenght_partString)
    
{
        
if(src[i] == partString[j])                                                //如果相等则继续
        {
            i
++;
            j
++;
        }

        
else                                                                                 //否则回溯
        {
            i 
= i - j + 1;
            j 
= 0;
        }

    }


    
if(j >= lenght_partString)                                                   //表示找到并返回
    {
        
return i - lenght_partString;
    }

    
else
        
return -1;
}



int  main()
{
    
char srcString[] = "egabcrtabcvabc";                                //定义主串
    char partString[] = "abc";                                                     //定义子串

    
int firstPos = PartPostion(srcString, partString, 6);
    cout
<<"第一次出现的位置为:"<<endl;
    cout
<<firstPos<<endl;

    
int currentPos = 0;
    
int length_src = strlen(srcString);
    
int lenght_partString = strlen(partString);
    
int i = 0;                                                                                 //用于保存出现子串的次数
    int count[10];                                                                        //用于保存第i次出现子串的位置
    
    
while(currentPos <= length_src)                                     //如果当前位置小于主串的长度
    {
        currentPos 
= PartPostion(srcString, partString, currentPos);
        
if(currentPos < 0)                                                           //主串中不包含子串则跳出循环
        {
            
break;
        }

        
else                                                                                  //记录第i次出现子串的位置
        {
            count[i
++= currentPos;       
        }

        currentPos 
+= lenght_partString;                                //在主串中滑动子串个字符后继续比较
    }

    cout
<<"出现的次数为:"<<i<<endl;
    cout
<<"出现的位置分别是:"<<endl;
    
for(int k = 0; k < i; k++)                                                        //分别输出出现的位置
        cout<<count[k]<<" ";
    
return 0;
}
以下是用Java写无重复字符串的最长子串并注释的代码: ```java import java.util.HashSet; public class LongestSubstringWithoutRepeatingCharacters { public static int lengthOfLongestSubstring(String s) { int n = s.length(); int ans = 0; HashSet<Character> set = new HashSet<>(); int i = 0, j = 0; while (i < n && j < n) { if (!set.contains(s.charAt(j))) { set.add(s.charAt(j++)); ans = Math.max(ans, j - i); } else { set.remove(s.charAt(i++)); } } return ans; } public static void main(String[] args) { String s = "abcabcbb"; int length = lengthOfLongestSubstring(s); System.out.println("The length of the longest substring without repeating characters is: " + length); } } ``` 注释如下: 1. `import java.util.HashSet;` 导入 HashSet 类。 2. `public class LongestSubstringWithoutRepeatingCharacters {` 定义一个名为 LongestSubstringWithoutRepeatingCharacters 的类。 3. `public static int lengthOfLongestSubstring(String s) {` 定义一个名为 lengthOfLongestSubstring 的静态方法,它接受一个字符串 s 作为参数,返回一个整数。 4. `int n = s.length();` 获取字符串 s 的长度。 5. `int ans = 0;` 定义一个变量 ans,用于存储最长子串的长度。 6. `HashSet<Character> set = new HashSet<>();` 创建一个 HashSet 对象 set,用于存储不重复的字符。 7. `int i = 0, j = 0;` 定义两个指针 i 和 j,分别指向子串的起始位置和结束位置。 8. `while (i < n && j < n) {` 循环遍历字符串 s,直到 i 或 j 超出字符串的范围。 9. `if (!set.contains(s.charAt(j))) {` 如果 set 中不包含 s.charAt(j) 这个字符。 10. `set.add(s.charAt(j++));` 将 s.charAt(j) 添加到 set 中,并将 j 向右移动一位。 11. `ans = Math.max(ans, j - i);` 更新最长子串的长度。 12. `} else {` 如果 set 中已经包含 s.charAt(j) 这个字符。 13. `set.remove(s.charAt(i++));` 将 s.charAt(i) 从 set 中移除,并将 i 向右移动一位。 14. `}` 结束 if-else 语句。 15. `}` 结束 while 循环。 16. `return ans;` 返回最长子串的长度。 17. `public static void main(String[] args) {` 定义一个名为 main 的静态方法,它接受一个字符串数组 args 作为参数。 18. `String s = "abcabcbb";` 定义一个字符串 s。 19. `int length = lengthOfLongestSubstring(s);` 调用 lengthOfLongestSubstring 方法,获取最长子串的长度。 20. `System.out.println("The length of the longest substring without repeating characters is: " + length);` 输出最长子串的长度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值