URAL 1889. Airport Announcements 模拟题

1889. Airport Announcements

Time limit: 1.0 second
Memory limit: 64 MB
Igor was bored of waiting in an airport lounge. Oceanic Airlines, a company he didn't like so much, delayed the departure of his flight, so he was late for the connection flight to Petrozavodsk, where a programming camp was to be held. Now he had to wait for long 300 minutes at the airport. Soon he heard a public announcement. Maybe, his flight had been canceled or, maybe, there were discounts on burgers at a nearby bar—Igor couldn't tell for sure. It seemed that the announcement had been repeated in several languages, but, strangely, there hadn't been Russian among them.
Igor recognized the language of some of the phrases he had heard. He assumed that the number of phrases in the announcement had been the same regardless of the language and that the announcement had been made at most once in each language. Help Igor to find the number of languages in which the announcement was made.

Input

The first line contains the total number  n of phrases Igor heard (2 ≤  n ≤ 1 000). In the  ith of the following  n lines you are given the language of the  ith phrase or the word “unknown” if Igor couldn't recognize the language. It is guaranteed that Igor could recognize the language of at least one of the phrases. The name of a language is a string of a length from four to twenty symbols consisting of lowercase English letters.

Output

Output the number of languages in which the announcement was made. If there are several answers, list them in ascending order. If there is no solution, output the string “Igor is wrong.”

Samples

input output
6
english
unknown
unknown
unknown
german
unknown
2 3 6
4
english
french
unknown
english
Igor is wrong.
3
zulu
zulu
zulu
1




题意:有一个公告是用多个国家的语言播放的。   比如有a,b,c三种语言。  那么这个公告可能是aabbcc,也可以是abc。

每一个语言  代表这个语言的一个单词。 每种语言在公告中的单词数是一样多的,如aabc,a语种 多了一个单词,是不行的。 

而且每种语言 的单词都是连续的,而且仅出现一次,如第二个案例中,english非连续得出现了两次,所以这种情况是不行的。

unknown可以 用任意语言替代。如果没有符合要求的情况,输出 Igor is wrong.。  有且有多种情况的话,输出每一种情况的语种数,按升序输出。


比如第一个案例:

6
english
unknown
unknown
unknown
german
unknown


english 用e 表示,german 用g表示。   我下面的c表示中文,实际也可以是任意其他语言。

输出的2 ,两种语言,情况是  eeeggg;

输出的3,说明有三种语言,情况是 eeccgg   

输出的6,说明有6种语言,情况可能是  eabcgd     其中abcd,各自代表不同的语言


做法:先离散化,把单词表示成数字,后面处理可以更快一点。然后枚举 所有可以被n除的情况。复杂度为 根号n。 然后处理每种情况

总的复杂度为 O(n*根号n)



#include <stdio.h> 
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <string>
using namespace std;

string str[1100];
string sss;
int numb[1100];
int yuyan[1100];
int vis[1100];
int n;
map<string ,int > my;

void judge(int i)
{
	if(n%i!=0)
		return ;
	int lun=n/i;//多少个单词一轮。
	int biao=-1;
	int ok=1;
	memset(vis,0,sizeof vis);
	for(int j=0;j<n;j++)
	{
		if(j%lun==0)
			biao=-1;
		if(numb[j]!=-1)//检测 这一段中 有无不同的单词
		{
			if(biao==-1)
				biao=numb[j];
			else if(biao!=numb[j])
				ok=0;
		}
		if((j+1)%lun==0&&biao!=-1)//检测不同组之间有无不同的单词
		{
			if(vis[biao]==0)
				vis[biao]=1;
			else 
				ok=0;
		}
		if(!ok)
			break;
	} 
	if(ok)
		yuyan[i]=1; 
}


void solve()
{
	int fang=(int)sqrt((double)n);
	for(int i=1;i<=fang;i++)//多少种 语音
	{
		if(n%i==0)
		{
			judge(i);
			judge(n/i);
		}
	}
}

int main()
{ 
	while(scanf("%d",&n)!=EOF)
	{
		my.clear();
		int nw=0;
		int flag=0;
		for(int i=0;i<n;i++)
		{
			cin>>str[i];

			if(str[i]!="unknown")
			{
				if(my.count(str[i])==0) 
				{
					numb[i]=nw;
					my[str[i]]=nw++;
				}
				else 
				{
					if(my[str[i]]!=nw-1) //间断出现 
						flag=1;// wrong
					numb[i]=nw-1;
				}
			}
			else
				numb[i]=-1;
		}
		memset(yuyan,0,sizeof yuyan);
		if(flag)
		{
			puts("Igor is wrong.");
			continue;
		}
		else
		{
			solve(); 
			int fir=1;
			for(int i=1;i<=n;i++)
			{
				if(yuyan[i]&&fir)
				{
					fir=0;
					printf("%d",i);
				}
				else if(yuyan[i])
					printf(" %d",i);
			}
			if(fir==1)
				puts("Igor is wrong.");
			else 
				puts("");
		}

	}
	return 0;
}



你可能感兴趣的:(模拟)