PTA-map(1100/1054/1071/1022)

1100 Mars Numbers (20 分)

People on Mars count their numbers with base 13:

Zero on Earth is called “tret” on Mars.
The numbers 1 to 12 on Earth is called “jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec” on Mars, respectively.
For the next higher digit, Mars people name the 12 numbers as “tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou”, respectively.
For examples, the number 29 on Earth is called “hel mar” on Mars; and “elo nov” on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.

Output Specification:

For each number, print in a line the corresponding number in the other language.

Sample Input:

4
29
5
elo nov
tam

Sample Output:

hel mar
may
115
13

思路

打表输出题,注意一些细节,比如13、26直接输出后面不带0.

#include<bits/stdc++.h>
using namespace std;
map<string,int> si;
string is[13]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string iis[12]={"tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
void init(){
	for(int i=0;i<13;i++){
		si[is[i]]=i;
	}
	for(int i=0;i<12;i++){
		si[iis[i]]=13*(i+1);
	}
}
int main()
{
	init();
	int n;
	cin>>n;
	getchar();
	for(int i=0;i<n;i++){
		string str;
		getline(cin,str);
		if(str[0]>='0'&&str[0]<='9'){
			int num=stoi(str);
			if(num<=12) cout<<is[num];
			else{
				int one=num/13;
				int two=num%13;
				if(two!=0) cout<<iis[one-1]<<' '<<is[two];
				else cout<<iis[one-1];
			}
		}else{
			int sum=0;
			if(str.size()>3){
				string str1=str;
				str1.erase(str1.begin()+3,str1.end());
				str.erase(str.begin(),str.begin()+4);
				sum=si[str1]+si[str];
			}
			else sum=si[str];
			cout<<sum;
		}
		cout<<endl;
	}
	return 0;
}

1054 The Dominant Color (20 分)

Behind the scenes in the computer’s memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the dominant color. A strictly dominant color takes more than half of the total area. Now given an image of resolution M by N (for example, 800×600), you are supposed to point out the strictly dominant color.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: M (≤800) and N (≤600) which are the resolutions of the image. Then N lines follow, each contains M digital colors in the range [0,2
24). It is guaranteed that the strictly dominant color exists for each input image. All the numbers in a line are separated by a space.

Output Specification:

For each test case, simply print the dominant color in a line.

Sample Input:

5 3
0 0 255 16777215 24
24 24 0 0 24
24 0 24 24 24

Sample Output:

24

思路

题意就是这是一张像素照片,找出数量多于总数一半的像素数,且保证像素数一定存在。
利用哈希表打表,最后输出最多的即可。

#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> mp;
int main()
{
	int m,n;
	cin>>m>>n;
	while(n--){
		for(int i=0;i<m;i++){
			int x;
			cin>>x;
			mp[x]++;
		}
	}
	int cmp=-1;
	int value;
	for(auto it=mp.begin();it!=mp.end();it++){
		if(it->second>cmp){
			cmp=it->second;
			value=it->first;
		}
	}
	cout<<value;
	return 0;
}

1071 Speech Patterns (25 分)

People often have a preference among synonyms of the same word. For example, some may prefer “the police”, while others may prefer “the cops”. Analyzing such patterns can help to narrow down a speaker’s identity, which is useful when validating, for example, whether it’s still the same person behind an online avatar.

Now given a paragraph of text sampled from someone’s speech, can you find the person’s most commonly used word?

Input Specification:

Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return \n. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].

Output Specification:

For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a “word” is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

Note that words are case insensitive.

Sample Input:

Can1: “Can a can can a can? It can!”

Sample Output:

can 5

思路

这是一道技巧性很强的题,题面很简单,参考了柳神的代码。
遍历字符串,如果是字母数字就添加到子串,如果不是就做一间断,将子串打表,并清除这个子串,注意空串不要计算在内。
isalnum(char c):如果c不是字母或数字,则返回0,否则返回非0值;
tolower(char c):如果c不是小写就返回小写,否则不变;
这两个函数减少了很多代码量以及思考量,需要刷题累积才能灵活使用。

#include<bits/stdc++.h>
using namespace std;
unordered_map<string,int> mp;
int main()
{
	string str,str1;
	getline(cin,str);
	str+=' '; 
	int last=0;
	for(int i=0;i<str.size();i++){
		if(isalnum(str[i])){
			str[i]=tolower(str[i]);
			str1+=str[i];
		} 
		else{
			if(str1.size()!=0) mp[str1]++;
			str1="";
		}
	}	
	int cmp=0;
	string res;
	for(auto it=mp.begin();it!=mp.end();it++){
		//cout<<it->first<<' '<<it->second<<endl;
		if(it->second>=cmp){
			cmp=it->second;
			res=it->first;
			if(it->first<res&&it->second==cmp){
				res=it->first;
			}
		}
	}
	cout<<res<<' '<<cmp;
	return 0;
} 

1022 Digital Library (30 分)

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID’s.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤10
4
) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:

Line #1: the 7-digit ID number;
Line #2: the book title – a string of no more than 80 characters;
Line #3: the author – a string of no more than 80 characters;
Line #4: the key words – each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
Line #5: the publisher – a string of no more than 80 characters;
Line #6: the published year – a 4-digit number which is in the range [1000, 3000].
It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.

After the book information, there is a line containing a positive integer M (≤1000) which is the number of user’s search queries. Then M lines follow, each in one of the formats shown below:

1: a book title
2: name of an author
3: a key word
4: name of a publisher
5: a 4-digit number representing the year

Output Specification:

For each query, first print the original query in a line, then output the resulting book ID’s in increasing order, each occupying a line. If no book is found, print Not Found instead.

Sample Input:

3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla

Sample Output:

1: The Testing Book
1111111
2222222
2: Yue Chen
1111111
3333333
3: keywords
1111111
2222222
3333333
4: ZUCS Print
1111111
5: 2011
1111111
2222222
3: blablabla
Not Found

思路

还是需要认真审题!key关键字不只有一个!
想要拿满分非常难这道题,测试点3的格式化输出是一个坑,printf("%07d\n",*it);必须使用格式化前补0作为输出。
测试点4常规会超时:我将map改为unordered_map还是超时,将cin改为scanf还是超时,其实是函数传参占了很多时间,提醒我们使用string或stl容器进行函数传参时,函数参数要写成引用的形式,会大大缩短时间。
其他需要注意的就是cin后的’\n’,要被getchar吸收,否则getline整行输入会吸收前一个回车导致不正确,这个应该要烂熟于心。

#include<bits/stdc++.h>
using namespace std;
unordered_map<string,set<int>> namemap,authormap,keymap,pubmap,yearmap;
void query(unordered_map<string,set<int>>& c,string& str){
	if(c.find(str)==c.end()){
		cout<<"Not Found"<<endl;
		return ;
	} 
	for(auto it=c[str].begin();it!=c[str].end();it++){
		printf("%07d\n",*it);
		//cout<<*it<<endl;
	}
}
int main()
{
	int n;
	int no;
	string name,author,key,pub,year;
	cin>>n;
	while(n--){
		cin>>no;
		getchar();
		getline(cin,name);
		namemap[name].insert(no);
		getline(cin,author);
		authormap[author].insert(no);
		while(cin>>key){
			keymap[key].insert(no);
			if(getchar()=='\n') break;
		}
		getline(cin,pub);
		pubmap[pub].insert(no);
		cin>>year;
		yearmap[year].insert(no);
	}
	int m;
	cin>>m;getchar();
	while(m--){
		string str;
		getline(cin,str);
		cout<<str<<endl;
		string str1=str.substr(3,str.size()-3);
		if(str[0]=='1') query(namemap,str1);
		if(str[0]=='2') query(authormap,str1);
		if(str[0]=='3') query(keymap,str1);
		if(str[0]=='4') query(pubmap,str1);
		if(str[0]=='5') query(yearmap,str1);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新西兰做的饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值