HDU 2594 next[len]应用
来源:互联网 发布:知聊可以提现吗 编辑:程序博客网 时间:2024/06/03 22:57
J - Simpsons’ Hidden Talents
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics, OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics, OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.
The lengths of s1 and s2 will be at most 50000.
clintonhomerriemannmarjorie
0rie 3
应用next[len],注意一个最大长度min(lena,lenb)。
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>#include<map>#include<string>using namespace std;const int maxlen = 1000100;int n;int m;int arrnext[maxlen+1];int res[maxlen + 1];char mai[maxlen];char sub[maxlen];void getnext(){int i = 0;int j = -1;arrnext[0] = -1;int len = strlen(sub);while (i <= len){if (j == -1 || sub[i] == sub[j]){j++;i++;arrnext[i] = j;//if (i-arrnext[i] != i&&i%(i-arrnext[i]) == 0)//{//printf("%d %d\n", i, i / (i - arrnext[i]));//}}elsej = arrnext[j];}//printf("%d\n",len%(len - arrnext[len])==0?len/(len - arrnext[len]):1);}void kmp(){getnext();int ans = 0;int i = 0;int j = 0;int lenmai = strlen(mai);int lensub = strlen(sub);while (i < lenmai){while (i < lenmai&&j < lensub){if (j == -1 || mai[i] == sub[j]){i++;j++;}elsej = arrnext[j];}if (j == lensub){j = 0;ans++;}}//whileprintf("%d\n", ans);}int t;int main(){//scanf("%d", &t);int key = 0;while (scanf("%s", sub)!=EOF){int lena = strlen(sub);scanf("%s", sub + strlen(sub));//printf("%s\n", mai);//continue;//int x;//scanf("%d", &x);//if (x == 0)//break;key++;//scanf("%s",mai);//scanf("%s", sub);//if (sub[0] == '.')//break;//printf("Test case #%d\n", key);//kmp();getnext();//printf("\n");int lensub = strlen(sub);int lenb = lensub - lena;//int ans = lensub - arrnext[lensub];//printf("%d\n", lensub%ans == 0 ? (lensub == ans ? ans : 0) : ans - (lensub%ans));int ans = arrnext[lensub];int ansmax = min(lena, lenb);if (ans > ansmax)ans = ansmax;if (ans != 0){for (int i = 0;i < ans;i++)printf("%c", sub[i]);printf(" ");}printf("%d\n", ans);}return 0;}
阅读全文
0 0
- HDU 2594 next[len]应用
- 最小循环节len-next[len]
- hdu 1358 next数组应用
- HDU 2594 Simpsons’ Hidden Talents KMP的next数组应用
- hdu-4763 kmp next数组的应用
- 【next数组应用】HDU 1358 Period
- hdu 4763 Theme Section (kmp next应用)
- hdu 3746 KMP next数组的应用
- hdu-1358 kmp,next数组的应用
- HDU 2752 kmp next的应用
- hdu 2594 Simpsons’ Hidden Talents KMP中next【】数组的应用
- HDU 2594 Simpsons’ Hidden Talents(KMP,next数组的应用)
- HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)
- len
- len
- HDU--杭电--1358--Period--KMP--next值的应用
- hdu 4763 Theme Section (kmp, Next数组的应用)
- HDU 3746 Cyclic Nacklace (next数组的应用)
- 《人人都是产品经理》——第三章笔记(上)
- Python起步之读写docx文件
- 影像质量评价指标汇总
- C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区
- 寻找连同块——油田问题 HDU 1241
- HDU 2594 next[len]应用
- Fedora设置静态ip
- 新建地图下载任务的六种方式
- R语言 使用sparklyr包连接spark,遇到的问题及解决
- 数日子
- VC2010中自定义消息
- 关于职业
- java中的位运算符
- 关于maven中版本控制问题