예제 #1
0
 int strStr(char *haystack, char *needle) {
 	if(haystack[0] == '\0'){
 		return needle[0] == '\0' ? 0 : -1;
 	}
 	if(needle[0] == '\0'){
 		return 0;
 	}
 	vector<int> next = genNext(needle);
     for(int i = 0, j = 0; ; i++, j++){
     	if(haystack[i] == '\0'){
     		return needle[j] == '\0' ? i - j : -1;
     	}
     	if(needle[j] == '\0'){
     		return i - j;
     	}
     	if(haystack[i] != needle[j]){
     		int jump = next[j];
     		if(next[j] != -1){
     			j = jump - 1;
     			i--;
     		}
     		else{
     			j = -1;
     		}
     	}
     }
     return -1;
 }
예제 #2
0
파일: countandsay.cpp 프로젝트: LuLuPan/lc1
  string countAndSay(int n) {
      string s("1");
      while(--n) {
          s = genNext(s);
      }
      
      return s;
 }
예제 #3
0
 string countAndSay(int n) {
   // Start typing your C/C++ solution below
   // DO NOT write int main() function
   string rlt = "1";
   while (--n > 0){
     rlt = genNext(rlt);
   }
   return rlt;
 }
예제 #4
0
파일: Pku_3461.c 프로젝트: budlion/ACM
int main(int argc,char *argv[])
{
	int k;
	scanf("%d\n",&k);
	while(k--){
		memset(T,0,sizeof(char)*T_LENGTH);
		memset(Next,-1,sizeof(int)*T_LENGTH);
		scanf("%s\n",T);
		genNext(T,Next); //计算匹配字符串所有子串的覆盖
		printf("%d\n",findString(T,Next));
	}
}
예제 #5
0
 /**
  * Returns a index to the first occurrence of target in source,
  * or -1  if target is not part of source.
  * @param source string to be scanned.
  * @param target string containing the sequence of characters to match.
  */
 int strStr(const char *source, const char *target) {
     if (target == NULL || source == NULL)
         return -1;
     if (target[0] == '\0')
         return 0;
     if (source[0] == '\0')
         return -1;
     int sl = static_cast<int>(strlen(source));
     int tl = static_cast<int>(strlen(target));
     vector<int> next(tl, 0);
     genNext(target, next);
     int i = 0, j = 0;
     while (i < sl && j < tl) {
         if (j == -1 || source[i] == target[j])
             i++, j++;
         else
             j = next[j];
     }
     return j >= tl ? i - j : -1;
 }