首页 > 代码库 > POJ - 3541 - Given a string…
POJ - 3541 - Given a string…
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 1819 | Accepted: 390 | |
Case Time Limit: 2000MS |
Description
Peter’s Boss is now very upset. He said that Peter’s vision of the orthogonal sum of two strings is not collinear to the general pary line of RIGS. At least, it is very bad that the orthogonal sum of two strings in Peter’s vision can be different depending on a selected set of strings. But Boss decided to give Peter a last str…well, a chance.
Peter’s colleague Andrew invented another definition of orthogonal sum of two strings of equal length n, which depends only on the alphabet. The basic alphabet to define this operation consists only of zeros and ones. The orthogonal sum of two strings a ⊕ b is just a string c where ci = ai ⊕ bi (Si denotes i-th character of string S). Here ⊕ stands for exclusive OR operation which returns 0 for equal characters and 1 otherwise.
Now Peter must study properties of orthogonal closure of a given string S. The orthogonal closure of S (denoted S⊕) is a set of strings S(k) ⊕ S(l) for any 0 ≤ k, l ≤ n − 1, where n is the length of S, and S(k) denotes an operation of k-th circular shift of S — moving k last characters from the end of the string S to its beginning. For example, the second circular shift of abcde is deabc.
Given a string T, Peter’s task is to check whether it belongs to S⊕. Could you solve this task for him?
Input
The first line of the input file contains a given string T. The second line contains S. Both strings are of equal length in range from 1 to 5 000 characters. All characters in these strings are zeros or ones.
Output
If a given string belongs to S⊕, output “Yes”. Otherwise output “No”.
Sample Input
#1 | 11111 10101 |
---|---|
#2 | 11110 10101 |
Sample Output
#1 | No |
---|---|
#2 | Yes |
Source
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define MAX 100002 5 #define ll long long 6 #define XOR(x,y) ( x==y ? ‘0‘ : ‘1‘) 7 using namespace std; 8 9 char s[MAX],t[MAX],e[MAX];10 int next[MAX];11 12 void get_next(char *p,int ls){13 int k,i;14 k=-1;i=0;15 memset(next,-1,sizeof(next));16 while(i<=ls-1){17 if(k==-1 || p[i]==p[k]){ k++; i++; next[i]=k;}18 else k=next[k];19 }20 }21 22 int kmp(int st,int ls,int lt){23 int i,j;24 i=0;j=0;25 for(int k=0;k<lt;k++) e[k]=XOR(s[st+k],t[k]);26 e[lt]=‘\0‘;27 get_next(e,lt);28 while(i<ls && j<lt){29 if(j==-1 || s[i]==e[j]){ i++; j++;}30 else j=next[j];31 }32 if(j>=lt) return (i-lt+1);33 return -1;34 }35 36 int main(){37 int f,lt,ls;38 //freopen("data.txt","r",stdin);39 while(scanf("%s",t)!=EOF){40 scanf("%s",s);41 lt=strlen(t);42 ls=strlen(s);43 for(int i=0;i<ls;i++) s[i+ls]=s[i];44 s[ls*2]=‘\0‘;45 f=-1;46 for(int i=0;i<ls;i++){47 f=kmp(i,ls*2,lt);48 if(f!=-1) break;49 }50 if(f!=-1) printf("Yes\n");51 else printf("No\n");52 }53 return 0;54 }
POJ - 3541 - Given a string…