vjudge寒假训练————第一周(上)

文章讲述了编程中的两个问题:使用数组离散化解决查找问题,以及判断字符串是否为D-unique和surprisingstrings。还介绍了如何利用map数据结构计算字符串出现频率以找出最流行的问题答案。

P - Where is the Marble?

题目大意:给你n个数,m次查询每次查找一个数x,你需要找出这n个数排序后x所在位置,如果有多个x,输出第一个位置

读完这道题立马想到数组离散化的思想代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int v[10010];
int a[10010];
int c[10010];
int main()
{
	int n,q;
	int cnt=1;
	while(cin>>n>>q)
	{
		if(n==0&&q==0)
		break;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		for(int i=1;i<=n;i++)
		{   if(v[a[i]]==0)
			{
				v[a[i]]=i;
			}
			
		}
		for(int i=1;i<=q;i++)
		{
			cin>>c[i];
		}
		cout<<"CASE# "<<cnt<<":"<<endl;
		for(int i=1;i<=q;i++)
		{
		    if(v[c[i]]==0)
		    {
		    	cout<<c[i]<<" not found"<<endl;
			}
			else
			{
				cout<<c[i]<<" found at "<<v[c[i]]<<endl;
			}
		}
	memset(a,0,sizeof(a));
	memset(c,0,sizeof(c));
	memset(v,0,sizeof(v));
	cnt++;
}
	return 0;
}

还可以排好顺序后用lower_bound()函数直接查找,感兴趣的读者可自行去了解

Q - Surprising Strings

The D-pairs of a string of letters are the ordered pairs of letters that are distance D from each other. A string is D-unique if all of its D-pairs are different. A string is surprising if it is D-unique for every possible distance D.

Consider the string ZGBG. Its 0-pairs are ZG, GB, and BG. Since these three pairs are all different, ZGBG is 0-unique. Similarly, the 1-pairs of ZGBG are ZB and GG, and since these two pairs are different, ZGBG is 1-unique. Finally, the only 2-pair of ZGBG is ZG, so ZGBG is 2-unique. Thus ZGBG is surprising. (Note that the fact that ZG is both a 0-pair and a 2-pair of ZGBG is irrelevant, because 0 and 2 are different distances.)

Acknowledgement: This problem is inspired by the "Puzzling Adventures" column in the December 2003 issue of Scientific American.

Input

The input consists of one or more nonempty strings of at most 79 uppercase letters, each string on a line by itself, followed by a line containing only an asterisk that signals the end of the input.

Output

For each string of letters, output whether or not it is surprising using the exact output format shown below.

Sample

InputcopyOutputcopy
ZGBG
X
EE
AAB
AABA
AABB
BCBABCC
*
ZGBG is surprising.
X is surprising.
EE is surprising.
AAB is surprising.
AABA is surprising.
AABB is NOT surprising.
BCBABCC is NOT surprising.

解题思路:判断字符串是不是令人惊讶的字符串,直接可以根据令人惊讶的字符串的性质模拟即可

数据范围小

代码:

#include<iostream>
#include<cstring>
using namespace std;
int flag;
int main()
{   char s[100];
	while(scanf("%s",s))
	{
		if(strcmp(s,"*")==0)
		break;
		int len=strlen(s);
		if(len<=2)
		{
			printf("%s is surprising.\n",s);
			continue;
		}
		for(int cnt=0;cnt<len-2;cnt++)
		{
			for(int i=0;i<len-2-cnt;i++)
		{
			for(int j=i+1;j<=len-2-cnt;j++)
			if(s[i]==s[j]&&s[i+cnt+1]==s[j+cnt+1]) 
			{
				flag=1;
				break;
			}
		}
		if(flag==1)
		{
			break;
		}
		}
		if(flag==0)
		printf("%s is surprising.\n",s);
		else
		printf("%s is NOT surprising.\n",s);
		flag=0;
	}
	return 0;
 } 

R - Let the Balloon Rise

Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.

Input

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.

Output

For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.

Sample

InputcopyOutputcopy
5
green
red
blue
red
red
3
pink
orange
pink
0
red
pink

题目大意+解题思路:找出出现次数最多的字符串,读过题后第一开始没有思路,后面想到字符串对应数字的结构可以用map构建关系,于是这道题就变得简单了。

代码:

#include<iostream>
#include<map>
using namespace std;
int n;
int main()
{
	while(cin>>n)
	{
		if(n==0)
		break;
		string s;
		map<string,int>m;
		for(int i=1;i<=n;i++)
		{
			cin>>s;
			if(m[s]!=0)
			{
				m[s]++;
			}
			else
			m[s]=1;
		 } 
		 map<string,int>::iterator it;
		 string s1;
		 int cnt=0;
		 for(it=m.begin();it!=m.end();it++)
		 {
		 	if(it->second>cnt)
			 {
			 	cnt=it->second;
			 	s1=it->first;
			  } 
		 }
		 cout<<s1<<endl;
	}
	return 0;
}

这篇博客写到这就完结了,完结撒花!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值