UVA261--The Window Property


The Window Property

Suppose you are given a sequence of symbols but you can see only k ( tex2html_wrap_inline45 ) 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值