【算法入门】字符串

字符串

https://ac.nowcoder.com/acm/problem/18386

Thinking Process

this question is relatively easy to beginners and friendly.Method is two pointers. when the number of characters between pointer 'l' and pointer has been up to 26 different letters, update the length and only move l to plus 1. It's not necessary to move 'r' pointer because 'l'~'r' range has been proved that it's legal.

Code

// ykjygvedtysvyymzfizzwkjamefxjnrnphqwnfhrnbhwjhqcgqnplodeestu
#include<iostream>
#include<string.h>
#include<math.h>
int num[26];
using namespace std;
string s;
int main() {
    cin >> s ;
    int len = s.length();
    int res = 0xffffff, cnt = 0, r = 0;
    for(int l = 0 ; l < len;l ++) {
        while(r < len && cnt != 26) {
            if(num[s[r] - 'a'] == 0) cnt ++;
     //       printf("现在的字母是%c现在的cnt=%d\n", s[r], cnt);
            num[s[r] - 'a'] ++;
            r ++;
        }
        if(cnt == 26) res = min(res, r - l);
   //     printf("res = %d\n", res);
        num[s[l] - 'a'] --;
        if(num[s[l] - 'a'] == 0) cnt --;
        
    }
    printf("%d", res);
} 
全部评论

相关推荐

点赞 评论 收藏
分享
人力小鱼姐:实习经历没有什么含金量,咖啡店员迎宾这种就别写了,其他两段包装一下 想找人力相关的话,总结一下个人优势,结合校园经历里有相关性的部分,加一段自我评价
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务