1889. Airport Announcements
Time limit: 1.0 second
Memory limit: 64 MB
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 |
Problem Author: Denis Dublennykh
Problem Source: NEERC 2011, Eastern subregional contest
细节题:
首先,从上到下,有几种语言,每种语言的行数一样,也就是说,可能性只有n的约数,因为n<=1000,所以并不会超时,不过这套题有很多的bug,改了六七遍,最后终于ac了。
对于每一个n/i来说,里面的语言只能有一种或者是未知的unknown。
其次,因为一开始我的代码的每一个分块是没有联系的,后来发现了这个问题。。。。
所以,任意的分块他们的语言是不能一样的,比如,english,unknown,unknown,english。这样的分块就不能是2.
所以要求出每一个分块的语言,如果有相同的就不行了。。。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <set>
#include <vector>
using namespace std;
string a[1010];
int n;
bool solve(int x) 求分块
{
for(int i=1; i<=n; i+=n/x)
{
set<string> p;
for(int j=i; j<i+n/x; j++)
{
string s=a[j];
if(s!="unknown")
{
if(!p.count(s)&&!p.empty()) return false; 判断能否正确分块
p.insert(s);
}
}
}
for(int i=n/x;i<=n;i+=n/x)
{
if(a[i]==a[i+1]&&(a[i]!="unknown"&&a[i+1]!="unknown")) return false;
}
return true;
}
int main()
{
while(cin>>n)
{
vector<string>vec;
for(int i=1; i<=n; i++)
{
cin>>a[i];
if(a[i]!="unknown")vec.push_back(a[i]); 进行特殊情况的判断,即两种相同的语言之间有另一种语言
}
bool ok=true;
for(int i=0; i<vec.size(); i++)
{
for(int j=i+1; j<vec.size(); j++)
{
if(vec[j]==vec[i])
{
for(int l=i+1; l<j; l++)
{
if(vec[l]!=vec[i]){ ok=false; break;}
}
}
}
}
if(ok==false)
{
cout<<"Igor is wrong."<<endl; 特殊情况的处理
}
else
{
set<int>z; string b[1010];
for(int i=1;i<=n;i++) b[i]="unknown";
for(int i=1; i<n; i++)
{
if(n%i==0)
{
if(i==1)
{
if(solve(i)) z.insert(i);
}
else
{
if(solve(i))
{
int sum=0; bool f=true;
for(int j=1;j<=n;j+=n/i)
{
bool o=true;
for(int k=j;k<j+n/i;k++)
{
if(a[k]!="unknown") {b[++sum]=a[k];o=false;break;}
}
if(o)b[++sum]="unknown";
}
for(int k=1;k<=sum;k++)
{
for(int j=k+1;j<=sum;j++)
{
if(b[k]==b[j]&&(b[k]!="unknown"&&b[j]!="unknown")) {f=false; break;}
}
}
if(f)z.insert(i);} 如果不同的分块有相同的语言就不行了
}
}
}
set<string>p1;
bool flag=true;
for(int i=1; i<=n; i++)
{
if(a[i]=="unknown") continue;
else if(!p1.count(a[i])) p1.insert(a[i]);
else{flag=false;}
}
if(flag) z.insert(n);对于n的情况的特殊处理
if(!z.size()) cout<<"Igor is wrong."<<endl;
else
{
set<int>::iterator it=z.begin();
cout<<*it;
for(it=++z.begin(); it!=z.end(); ++it)
cout<<" "<<*it;
cout<<endl; 输出
}
}
}
return 0;
}