Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

不断调用strlen()会TLE,所以要存下来

Posted by yousiki at 2016-07-24 11:50:22 on Problem 3461
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1000005;
int n, ans, next[N];
char str1[N], str2[N];
signed main(void) {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%s", str1); int len1 = strlen(str1);
		scanf("%s", str2); int len2 = strlen(str2);
		memset(next, 0, sizeof(next)), ans = 0;
		for (int i = 1, j = 0; i < len1; i++) {
			while (j > 0 && str1[i] != str1[j])j = next[j];
			if (str1[i] == str1[j])j++;
			next[i + 1] = j;
		}
		for (int i = 0, j = 0; i < len2; i++) {
			while (j > 0 && str1[j] != str2[i])j = next[j];
			if (str1[j] == str2[i])j++;
			if (j == len1)ans++, j = next[j];
		}
		printf("%d\n", ans);
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator