| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
不管怎么是ms kmp不是这么写的……In Reply To:必胜客在召唤^_^ Posted by:outstanding at 2006-01-23 22:13:14 > 我很努力地学习和使用KMP函数,为什么从TLE到WA就是不AC捏?
> 这样吧,谁能教我AC我请他吃必胜客好吗?
> #include <iostream>
> #include <string>
> using namespace std;
> string s;
> char c;
> int next[400002],len, ans[400002];
>
> void get_next()
> {
>
> int i, j;
> i = 0;
> j = -1;
> next[0] = -1;
> next[1] = 0;
> while(i <= len)
> {
> if(j == -1 || s[i] == s[j])
> {
> j++;
> i++;
> next[i] = j;
> }
> else j=next[j];
> }
>
> }
>
>
> int main()
> {
> int i, j;
> s = "";
> while(scanf("%c", &c) != EOF)
> {
> if(c != '\n')
> {
> s += c;
> }
> else
> { len = s.size();
> memset(next, 0, sizeof(next));
> get_next();
> i = 0;
> while(len)
> {
> ans[i++] = len;
> len = next[len];
> }
> for(j= i-1; j >= 0; j--)
> {
> if(j != i-1)
> printf(" ");
> printf("%d", ans[j]);
> }
> printf("\n");
> s = "";
> }
> }
> return 0;
> }
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator