题目:
Questions
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1355 | Accepted: 491 |
Description
Holding a collegiate programming contest is a very exhausting work. There is a well-known proverb that one fool can ask so many questions that a hundred clever men will not answer. And during a collegiate programming contest questions are asked by one hundred clever people.
The jury of the Third Urals Collegiate Programming Contest being clever enough has found a simple way to make its work easier. We have invented a simple algorithm that will help us answer ALL your numerous questions! Moreover, this algorithm guarantees that the same questions will have the same answers (this would be hardly possible, if we would undertook such a task ourselves). According to this algorithm a member of the jury starts to delete characters of the question in the following order:
You should help the jury and write a program that will do a hard work of answering your questions tomorrow. The number N is secret and will not be announced even after the end of the contest. Your program should use N=1999.
For example, taking a string "Is it a good question?" (its length is 22) the characters will be counted in the following way: "Is it a good question?Is it ... quest" and "i" will be deleted. Then the count restarts from "on?Is it..." etc., until "s" will be left (thus the answer is "No comments", as usual).
The jury of the Third Urals Collegiate Programming Contest being clever enough has found a simple way to make its work easier. We have invented a simple algorithm that will help us answer ALL your numerous questions! Moreover, this algorithm guarantees that the same questions will have the same answers (this would be hardly possible, if we would undertook such a task ourselves). According to this algorithm a member of the jury starts to delete characters of the question in the following order:
- Starting from the first character he or she counts out N-1 characters (spaces, punctuation marks etc. are considered to be characters too) and deletes the Nth character.
- If a string ends the count continues from the beginning of the string.
- After deleting a character the count restarts from the character that would be the (N+1)-st in the previous count.
- If the last remaining character is a question-mark ("?") then the answer to the question is "Yes". If it is a space then the answer is "No". Any other character will lead to "No comments" answer.
You should help the jury and write a program that will do a hard work of answering your questions tomorrow. The number N is secret and will not be announced even after the end of the contest. Your program should use N=1999.
For example, taking a string "Is it a good question?" (its length is 22) the characters will be counted in the following way: "Is it a good question?Is it ... quest" and "i" will be deleted. Then the count restarts from "on?Is it..." etc., until "s" will be left (thus the answer is "No comments", as usual).
Input
The input is a question, that is any text file containing at least one character (end of line is not a character). Each character of the input (excepting the ends of lines) is a part of the question.
The size of the input file is not more than 30000.
The size of the input file is not more than 30000.
Output
The answer.
Sample Input
Sample input #1 Does the jury of this programming contest use the algorithm described in this problem to answer my questions? Sample input #2 At least, will anybody READ my question? Sample input #3 This is UNFAIR!
Sample Output
Sample output #1 Yes Sample output #2 No Sample output #3 No comments
Hint
there are no spaces in the sample inputs except for those between words in one line. Thus the first question contain 108 characters, the second — 40 and the third — 14.
Source
Ural Collegiate Programming Contest 1999,trial tour
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
char cha;
typedef struct point
{
int le,ri;
int mid()
{
return (le+ri)/2;
}
char ch;
int numch;
} poi;
poi tree[120050];
int builttree(int l,int r,int root)
{
if(l==r)
{
tree[root].le=l;
tree[root].ri=r;
tree[root].numch=0;
return 0;
}
tree[root].le=l;
tree[root].ri=r;
builttree(l,(l+r)/2,root*2);
builttree((l+r)/2+1,r,root*2+1);
return 0;
}
int charu(int root,int jb,char c)
{
if(tree[root].le==tree[root].ri)
{
tree[root].numch=1;
tree[root].ch=c;
return 0;
}
if(tree[root].mid()<jb)
charu(root*2+1,jb,c);
else
charu(root*2,jb,c);
tree[root].numch=tree[root*2].numch+tree[root*2+1].numch;
return 0;
}
int delet(int root,int p)
{
if(tree[root].le==tree[root].ri)
{
tree[root].numch=0;
// cout<<tree[root].ch;
return 0;
}
if(p>tree[root*2].numch)
delet(root*2+1,p-tree[root*2].numch);
else
delet(root*2,p);
tree[root].numch=tree[root*2].numch+tree[root*2+1].numch;
return 0;
}
char chaxun(int root,int p)
{
if(tree[root].le==tree[root].ri)
{
cha=tree[root].ch;
return 0;
}
if(p>tree[root*2].numch)
chaxun(root*2+1,p);
else
chaxun(root*2,p);
return 0;
}
int main()
{
int i,cnt,djb,sjb;
char s[30005];
cnt=0;
builttree(1,30001,1);
while(gets(s))
{
for(i=0;s[i];i++)
{
charu(1,++cnt,s[i]);
}
}
djb=cnt;
sjb=1;
for(;djb>1;djb--)
{
sjb=(sjb-1+1999)%djb;
if(sjb==0) {delet(1,djb);sjb=1;}
else {delet(1,sjb);}
}
chaxun(1,1);
//cout<<cha<<endl;
if(cha=='?') printf("Yes\n");
else if(cha==' ') printf("No\n");
else printf("No comments\n");
return 0;
}
说明:此题的输入很坑,输入有多行,换行符不计算在内,结束行的内容不是字符,因此可以用GETS函数处理,这题一开始觉得一般的处理会超时,就放弃了,改用了线段树,结果还好,没有超空间