KMP算法
来源:互联网 发布:少年派的奇幻漂流知乎 编辑:程序博客网 时间:2024/06/06 01:55
Alice and Bob is playing a game.
Each of them has a number. Alice’s number is A, and Bob’s number is B.
Each turn, one player can do one of the following actions on his own number:
1. Flip: Flip the number. Suppose X = 123456 and after flip, X = 654321
2. Divide. X = X/10. Attention all the numbers are integer. For example X=123456 , after this action X become 12345(but not 12345.6). 0/0=0.
Alice and Bob moves in turn, Alice moves first. Alice can only modify A, Bob can only modify B. If A=B after any player’s action, then Alice win. Otherwise the game keep going on!
Alice wants to win the game, but Bob will try his best to stop Alice.
Suppose Alice and Bob are clever enough, now Alice wants to know whether she can win the game in limited step or the game will never end.
First line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.
For each test case: Two number A and B. 0<=A,B<=10^100000.
For each test case, if Alice can win the game, output “Alice”. Otherwise output “Bob”.
411111 11 1111112345 54321123 123
AliceBobAliceAlice
For the third sample, Alice flip his number and win the game.
For the last sample, A=B, so Alice win the game immediately even nobody take a move.
题意:如果 后面的串 是 前面的串 的子串 ,或者是 前面串 翻转后 的子串,则输出Alice,否则输出Bob;
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int next[1000000];string a,b;void GetNext(){ int len_p = a.size(); next[0] = -1; int k = -1,j = 0 ; while(j<len_p) { if(k == -1||a[j] == a[k]) { ++k; ++j; next[j] = k; } else k = next[k]; }}int KMP(){ int ans=-1,i = 0,j = 0; int len_p = a.size(),n=b.size(); while(i<n) { if(j==-1||b[i] == a[j]) { ++i; ++j; } else j = next[j]; if(j == len_p) { return i-len_p; break; } } return ans;}int main(){ int n,m,i,j; int t; scanf("%d",&t); while(t--) { cin>>b>>a; if(a=="0") { printf("Alice\n"); continue; } if(b.size()<a.size()) { printf("Bob\n"); continue; } GetNext(); m=KMP(); reverse(b.begin(),b.end()); n=KMP(); if(n!=-1||m!=-1) { printf("Alice\n"); } else { printf("Bob\n"); } } return 0;}
心得:next数组表示的是一个字符串前缀和后缀最大的公共长度,比如字符串s= abcabca 这个字符串 ,其next[0]=-1,next[1]=0,next[2]=0,next[3]=0,next[4]=1,
next[5]=2,next[6]=3,next[7]=4,我们发现next[0]=-1,可是长度是不会为负数的,所以next[0]不用来表示s[0],而是用next[1]来表示s[0],那next[2]自然而然就是表示s[1]的,以此类推......所以这里求next数组的那个while()循环是i<len,而不是i<len-1,这和求子串不一样,求子串的话随便哪个都行,求循环节的话必须用i<len,所以最好用i<len
总结扩展知识,需深入理解:
KMP算法next数组的一些求解:
void GetNext(){ int len_p=strlen(patten); next[0]=-1; int k=-1,j=0; while(j<len_p) { if(k == -1||patten[j] == patten[k]) { ++k; ++j; next[j] = k; } else k = next[k]; }}
public static int[] getNext(String t) {int[] next = new int[t.length()];next[0] = -1;int suffix = 0; // 后缀int prefix = -1; // 前缀while (suffix < t.length() - 1) {//若前缀索引为-1或相等,则前缀后缀索引均+1if (prefix == -1 || t.charAt(prefix) == t.charAt(suffix)) {++prefix;++suffix;next[suffix] = prefix; //1 } else {prefix = next[prefix]; //2}}return next;}
void get_nextval(const char *T, int next[]){// 求模式串T的next函数值并存入数组 next。 int j = 0, k = -1; next[0] = -1; while ( T[j] != '/0' ) { if (k == -1 || T[j] == T[k]) { ++j; ++k; if (T[j]!=T[k]) next[j] = k; else next[j] = next[k]; } else k = next[k]; }}
next数组优化:
public static int[] getNext(String t) {int[] next = new int[t.length()];next[0] = -1;int suffix = 0; // 后缀int prefix = -1; // 前缀while (suffix < t.length() - 1) {//若相等或前缀索引为-1,则前缀后缀索引均+1if (prefix == -1 || t.charAt(prefix) == t.charAt(suffix)) {++prefix;++suffix;//改进的地方if (t.charAt(prefix) == t.charAt(suffix)) {next[suffix] = next[prefix];} else {next[suffix] = prefix;}} else {prefix = next[prefix]; }}return next;}
KMP模式匹配算法:
public static int KMP(String s, String t) {int i = 0;int j = 0;//得到next数组int[] next = getNext(t);while (i < s.length() && j < t.length()) {if (j == -1 || s.charAt(i) == t.charAt(j)) {i++;j++;} else {//根据next数组的指示j进行回溯,而i永远不会回溯j = next[j];}}if (j == t.length()) {return i - j;} else {return -1;}}
int KMP(const char *Text,const char* Pattern) //const 表示函数内部不会改变这个参数的值。{ if( !Text||!Pattern|| Pattern[0]=='\0' || Text[0]=='\0' )// return -1;//空指针或空串,返回-1。 int len=0; const char *c=Pattern; while(*c++!='\0')//移动指针比移动下标快。 ++len;//字符串长度。 int *next=new int[len+1]; get_nextval(Pattern,next);//求Pattern的next函数值 int index=0,i=0,j=0; while(Text[i]!='\0' && Pattern[j]!='\0' ) { if(Text[i]== Pattern[j]) { ++i;// 继续比较后继字符 ++j; } else { index += j-next[j]; if(next[j]!=-1) j=next[j];// 模式串向右移动 else { j=0; ++i; } } } delete []next; if(Pattern[j]=='\0') return index;// 匹配成功,返回匹配首字符下标 else return -1;}
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- KMP算法
- Struts2 Part1
- Error creating bean with name 'entityManagerFactory' defined in class path resource
- 愿你走出半生,归来仍是少年
- C++ 命令行模式
- IO-3、文件分配块的三种方式
- KMP算法
- UVA 10285 Longest Run on a Snowboard(记忆化搜索|动态规划入门)
- 程序员必须掌握的8大排序算法
- IntelliJ IDEA破解教程
- Comment 的使用
- Nginx更改源码隐藏软件名和版本号
- spring data 生成表结构时修改字段类型
- 第一篇博客
- Oaecle 命令行基础操作篇(6)