[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher K HDU 3336
2016-11-04 13:46
477 查看
题目地址:https://vjudge.net/contest/70325#problem/K
思路:先求next,如果该位置next不为0,则说明有一个字串又出现了一次,所以总答案+2,没有的话就只有该字串自身的一次,所以总答案+1。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
char t[maxn];
int Next[maxn],tlen;
void getNext()
{
int j,k;
j=0,k=-1,Next[0]=-1;
while(j<tlen)
{
if(k==-1 || t[j]==t[k])
Next[++j]=++k;
else
k=Next[k];
}
}
int main()
{
int caset;
scanf("%d",&caset);
while(caset--)
{
scanf("%d",&tlen);
scanf("%s",t);
getNext();
int ans=0;
for(int i=1;i<=tlen;i++)
{
ans+=Next[i]?2:1;
ans%=10007;
}
printf("%d\n",ans);
}
}
思路:先求next,如果该位置next不为0,则说明有一个字串又出现了一次,所以总答案+2,没有的话就只有该字串自身的一次,所以总答案+1。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
char t[maxn];
int Next[maxn],tlen;
void getNext()
{
int j,k;
j=0,k=-1,Next[0]=-1;
while(j<tlen)
{
if(k==-1 || t[j]==t[k])
Next[++j]=++k;
else
k=Next[k];
}
}
int main()
{
int caset;
scanf("%d",&caset);
while(caset--)
{
scanf("%d",&tlen);
scanf("%s",t);
getNext();
int ans=0;
for(int i=1;i<=tlen;i++)
{
ans+=Next[i]?2:1;
ans%=10007;
}
printf("%d\n",ans);
}
}
相关文章推荐
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher C HDU 2087
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher B HDU 1686
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher A HDU 1711
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher J HDU 2594
- 【 题集 】 【kuangbin带你飞】专题十六 KMP & 扩展KMP & Manacher
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher L(kmp扩展)
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G POJ 2406
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher J (kmp扩展)
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher(1) A-H
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher H POJ 2752
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher E HDU 1358
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher F HUST 1010
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher 题目分析
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher K (kmp扩展)
- [kuangbin带你飞]专题十六 KMP & 扩展KMP
- 最小(大)表示法习题 -- 来自[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher
- HDU 1495 & [kuangbin带你飞]专题一 简单搜索 M
- [kuangbin带你飞]专题一 简单搜索 G - Shuffle'm Up poj 3087
- [kuangbin带你飞]专题二 搜索进阶 I - A计划(HDU 2102)