The Window Property
The Window Property |
Suppose you are given a sequence of symbols but you can see only k ( ) consecutive symbols at a time. Then we say the length of the window is k. Moving this window along the sequence can give you a lot of different patterns. Of all possible sequences ofn different symbols only a minority has the property that the windows of lengthk show only k+1 different patterns.
We say that a sequence of symbols has thewindow property if for all naturalk the number of different patterns you can see through a window of lengthk is at most k+1.
Examples
ABAABABAB has the window property.ABCABCABC does not have the window property (check k=1).
011010 has the window property.
0110100101 does not have the window property.
In the third example the patterns axe :
length 1 : 0, 1.length 2 : 01, 11, 10.
length 3 : 011, 110, 101, 010.
length 4 : 0110, 1101, 1010.
length 5 : 01101, 11010.
length 6 : 011010.
The sequence in the last example is an extension of the sequence in the third example. So the first 6 symbols form a sequence with the window property. The seventh symbol adds the pattern '00' to the set of patterns of length 2 displayed in the windows preceding the window containing '00'. So the sequence formed by the first 7 symbols does not have the window property. Accordingly we call the seventh symbol the first offending symbol. By the way we count from left-to-right as our computers seem to do.
The problem is to determine whether a given sequence has the window property and if not, to find the position of the first offending symbol - this is that symbol such that the sequence preceding it has the window property but adding the symbol destroys this property (counting of the symbols starts at one).
Input
The input is a textfile where each line is a non-empty sequence of (ASCII) characters to be checked for the window property. No sequence will be longer than one hundred symbols.
Output
The output file should be a textfile containing for each line of the input a line with the result of the check for the window property in the following way: `YES' (uppercase) if the line enjoys the window property, otherwise `NO:' (in uppercase) followed by the position of the offending symbol. Each line should be terminated by an end-of-line marker.
Sample Input
ababcababa 0010100100 0010101001
Sample Output
NO:5 YES YES
又是WA得莫名其妙、、、再来DEBUG。。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define maxn 100008 int ans; char str[maxn]; struct Trie { int first[maxn],nxt[maxn],ch[maxn]; int cnt; bool val[maxn]; void init() { val[0] = 1; first[0] = -1; cnt = 1; } int idx(char c) { return c; } void insert(char * s,int len) { int u = 0; for(int i = 0;i < len;i++) { bool flag = false; int c = idx(s[i]); int j; for(j = first[u];j != -1;j = nxt[j]) { if(c == ch[j]) { flag = true; break; } } if(!flag) { ch[cnt] = c; first[cnt] = -1; val[cnt] = 0; nxt[cnt] = first[u]; first[u] = cnt; cnt++; } if(flag) u = j; else u = cnt - 1; } if(val[u] == 0) ans++; val[u] = 1; } }trie; bool Judge(int len) { bool flag = true; for(int i = 1;i <= len/2+1;i++) { trie.init(); ans = 0; for(int j = 0;j <= len-i;j++) { trie.insert(str+j,i); if(ans > i+1) { flag = false; return flag; } } } return flag; } int main() { //freopen("in.txt","r",stdin); while(scanf("%s",str)!=EOF) { bool flag = true; int pos; int len = strlen(str); //枚举k从1到k/2+1 for(int i = 1;i <= len;i++) { if(!Judge(i)) { flag = false; pos = i; break; } } if(flag) printf("YES\n"); else printf("NO:%d\n",pos); } return 0; }