洛谷 P1603 斯诺登的密码

我一开始还没看懂非正规数字的意义,以为那里写的单词不算,蒙了好久,而且这题非常考验仔细程度,一不小心就RE,WA。

嗯,好像讲了些废话,那我们看看思路,我的做法和前面的大佬们有些不同,因为这题只有六个字母,很多人都是单个判断它们是不是26种(one~twenty)情况中的一种,但是如果数据给的多了(超过了6个单词)感觉效率会不高;我们可以反过来。

首先,我们可以在26个单词(one~twenty)前后加一个空格,因为题目给的句子中单词前后都有空格(句子最前面要手动加一个空格。然后我们就可以用 find 函数对26种情况中的每个单词在句子中出现的次数进行累加。然后在排列输出(对应数字小的一定在前面)

下面给上代码:

#include<bits/stdc++.h>
using namespace std;
string a[27]={""," one "," two "," three "," four "," five "," six "," seven "," eight "," nine "," ten "," eleven "," twelve ", " thirteen ", " fourteen ", " fifteen ", " sixteen ", " seventeen ", " eighteen ", " nineteen ", " twenty "," a "," both "," another "," first "," second "," third "};
int s[7],i,j,b,t=1;
int main(){
	getline(cin,a[0]);
	a[0]=' '+a[0];//在最前面加空格。 
	for(i=1;i<=26;i++){
		b=0;
		while(a[0].find(a[i],b)!=string::npos){//搜索出现次数 
			t++;//出现一次,累加一次。 
			b=a[0].find(a[i],b)+1;//从下一个编号搜索下一个单词 
		}
		for(;j<t;j++) s[j]=((i-1)%20+1)*((i-1)%20+1)%100;//计算对应数字 
	}
	sort(s+1,s+t);//排序 
	cout<<s[1];//第一个不需要补空格 
	for(i=2;i<t;i++)
		printf("%.2d",s[i]);//补空格输出 
	return 0;
}
posted @ 2018-10-30 20:18  一只不咕鸟  阅读(579)  评论(0编辑  收藏  举报