HDU 2087 플 라 워 테이프 (첫 번 째 KMP 문제)
3515 단어 HDU
#include <iostream>
#include <string>
using namespace std;
#define L1 1001
#define L2 1001
char s[L1], p[L2];
int nxt[L2], l1, l2;
int res;
void GetNext()
{
int k = -1, j = 0;
nxt[0] = -1;
while (j < l2)
{
if (k == -1 || p[j] == p[k])
{
j++, k++;
if (p[j] != p[k])
{
nxt[j] = k;
}
else
{
nxt[j] = nxt[k];
}
}
else
{
k = nxt[k];
}
}
}
int Kmp(int pos)
{
int i = pos, j = 0;
while (i < l1 && j < l2)
{
if (j == -1 || s[i] == p[j])
{
i++, j++;
}
else
{
j = nxt[j];
}
if (j == l2)
{
res++;
j = 0;
}
}
return res;
}
int main()
{
while (scanf("%s", s), s[0] != '#')
{
scanf("%s", p);
l1 = strlen(s);
l2 = strlen(p);
GetNext();
res = 0;
printf("%d
", Kmp(0));
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
hdu4671(다교리그 7--수 시뮬레이션)클릭하여 링크 열기 제목: n과 서버, m개의 데이터베이스가 있고 모든 데이터베이스는 서버를 연결해야 하지만 모든 데이터베이스는 서버를 연결하는 우선순위가 있습니다.모든 데이터베이스의 서버 우선순위를 구하다.또한 한...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.