HUST 1010 The Minimum Length
题目传送:The Minimum Length
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
char s[1000005];
int s_next[1000005];
int main() {
while(scanf("%s", s) != EOF) {
memset(s_next, 0, sizeof(s_next));
s_next[0] = -1;
int i = 0, j = -1;
int len = strlen(s);
while(i < len) {
if(j == -1 || s[i] == s[j]) s_next[++ i] = ++ j;
else j = s_next[j];
}
printf("%d\n", len - s_next[len]); //即输出最小循环节
}
return 0;
}
POJ 2406 Power Strings
题目传送:Power Strings
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
char s[1000005];
int s_next[1000005];
int main() {
while(scanf("%s", s) != EOF) {
if(strcmp(s, ".") == 0) break;
memset(s_next, 0, sizeof(s_next));
s_next[0] = -1;
int len = strlen(s);
int i = 0, j = -1;
while(i < len) {
if(j == -1 || s[i] == s[j]) s_next[++ i] = ++ j;
else j = s_next[j];
}
if(len % (len - s_next[len]) == 0) //特判
printf("%d\n", len / (len - s_next[len]));
else printf("1\n");
}
return 0;
}
POJ 2752 Seek the Name, Seek the Fame
题目传送:Seek the Name, Seek the Fame
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
char s[400005];
int s_next[400005];
void fun(int len) {
if(len <= 1) return;
fun(s_next[len]);
if(s_next[len] != 0)printf("%d ", s_next[len]);
}
int main() {
while(scanf("%s", s) != EOF) {
s_next[0] = -1;
int len = strlen(s);
int i = 0, j = -1;
while(i < len) {
if(j == -1 || s[i] == s[j]) s_next[++ i] = ++ j;
else j = s_next[j];
}
fun(len);//递归打印
printf("%d\n", len);
}
return 0;
}
HDU 2594 Simpsons’ Hidden Talents
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
char s1[100005];
char s2[100005];
int s_next[100005];
int main() {
while(scanf("%s %s", s1, s2) != EOF) {
int len1 = strlen(s1);
int len2 = strlen(s2);
for(int i = len1, j = 0; j <= len2; i ++, j ++) {
s1[i] = s2[j];
}
// printf("%s\n", s1);
int len = len1 + len2;
s_next[0] = -1;
int i = 0, j = -1;
int mi = min(len1, len2);
while(i < len) {
if((j == -1 || s1[i] == s1[j]) && j < mi) { //最大前缀后缀只能算小于mi的,大于等于mi就不对了,因为是比较两个字符串的前缀和后缀
s_next[++ i] = ++ j;
}
else j = s_next[j];
}
int t = s_next[len];
if(t > 0) {
for(int i = 0; i < t; i ++) {
printf("%c", s1[i]);
}
printf(" %d\n", t);
}
else printf("0\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-13 20:47:56