【约瑟夫环】Ural1098

本文介绍了一种使用约瑟夫环算法自动回答问题的方法,该算法通过特定的字符删除规则来处理输入问题,最终根据剩余字符给出答案。文章还提供了实现这一算法的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1098. Questions

Time limit: 1.0 second
Memory limit: 64 MB
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:
  1. 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.
  2. If a string ends the count continues from the beginning of the string.
  3. After deleting a character the count restarts from the character that would be the (N+1)-st in the previous count.
  4. 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 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. You should read question from input.
The size of the input is not more than 30000.

Output

The answer.

Samples

input output
Does the jury of this programming contest use the
algorithm described in this problem to answer my questions?
Yes
At least, will anybody READ my question?
No
This is
UNFAIR!
No comments

Hint

There are no spaces in the sample inputs except for those between words in one line. Thus the first question contains 108 characters, the second contains 40 and the third contains 14.

简单 约瑟夫环问题。

首先关于约瑟夫环的问题已经有人总结了数学公式,相当简单了,所以直接盗用就可以了。。

g(n,k)=(g(n,k)+k)%n,g(1,k)=0; 在这里连数组都不用,因为中间结果没用。然后就是关于这个公式的前生与今世辣。。

首先引入一个问题,总共n个人围成圈,从0开始报数,报到m-1的人退出,然后继续报数,问,最后剩下谁?

那么首先原始序列(假设m=4)

0,1,2,3,4,5,6,7,8,9,。。。。n

0,1,2,3

                     0,1,2,3,4,5,。。。 。n-1

                +  m  ,+m,+m,................................+m

(红色字体退出,然后重新计数,前面的移到后面)

那么重新计数的序列就变成了n-1的约瑟夫环了,所以就转变成子问题了。同样,子问题加上m后的序列号就是以前的序列号,所以就得到了g(n,k)=(g(n,k)+k)%n,g(1,k)=0;的公式。

//约瑟夫环的问题,变成字符串的处理了,也是醉了。。。
//首先来一发公式,g(n,k)=(g(n,k)+k)%n,g(1,k)=0;
//  
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include<cstring>
#include <algorithm>
#include <math.h>
using namespace std;
char ch[30005],c[30005];
const int N =1999;
int main()
{
	//freopen("q.in","r",stdin);
	int len=0,cnt=0,g=0; 
	while(gets(c)) //样例显然是可以换行的 
	{
		 strcpy(ch+len,c);
		 len+=strlen(c); //strlen()是不计算结尾的符的 
	}
   //	printf("%s\n",ch);
	//然后进行约瑟夫环的计算 
	//cout<<len<<endl;
    for(int i=2;i<=len;i++)
     g=(g+N)%i;
     
	// cout<<g<<endl;
	if(ch[g]==' ')cout<<"No"<<endl;
	else if(ch[g]=='?')cout<<"Yes"<<endl;
	else cout<<"No comments"<<endl;
	
	 
} 


渣渣也要努力的啊。。。



  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值