UVA12820 Cool Word 的题解

没有检索到标题
该文章是关于UVA在线判题系统中的一道题目UVA12820的解答,主要涉及如何判断一个单词是否为CoolWord,即单词由不同字母组成且每个字母出现次数不同。文章提供了使用C++的暴力解决方法,通过对每个单词的字母出现次数进行统计和比较来找出CoolWord的数量。

UVA12820 Cool Word 的题解

UVA传送门

题目大意

很酷的单词指的是一个单词至少由两个不同字母组成,且每个不同的字母出现次数都不相同。

最多有 30 30 30 次询问,每次询问给出 n n n 个单词( n ≤ 10000 n \leq 10000 n10000),请问这 n n n 个单词中有几个“很酷的单词”。

对于每次询问,请输出询问的编号和“很酷的单词”的数量。

  • 定义“单词”为至少有两个不同小写字母的字符串。

  • 定义 Cool Word 为在一个单词中每个字母出现的次数均不同的单词。

  • 给定 n n n 个字符串,求这 n n n 个字符串中 Cool Word 的个数。

思路

这题运用字符串和暴力的方法,判断是否由两个不同字母组成,且每个不同的字母出现次数不同。最后把出现的次数排序,再判断是否出现重复的。如果没有出现重复就把 a n s + 1 ans+1 ans+1。最后输出询问的编号和 a n s ans ans

分析样例:

第一行输入了一个 2 2 2 就连续输入了 2 2 2 个单词,输出的 Case 1: 1 就是第一个数据 ada 与 bbacccd 中只有一个符合 cool 词的单词。

然而第二行输出却一个 cool 词也没有:

首先分析第二次出入的第一个词:首先 illness 中的字母 i i i n n n e e e 数量都是 1 1 1 ——从此点上看就不对了;

而字母 l l l 有两个,可字母 s s s 也有 2 2 2 个,就更不符合了。

再来分析第二个单词:它整个单词都是 a a a,所以就判断为非 cool 词。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <climits>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <ctime>
#include <string>
#include <cstring>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
	inline int read() {
		register int x = 0, f = 1;
		register char c = getchar();
		while (c < '0' || c > '9') {
			if(c == '-') f = -1;
			c = getchar();
		}
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
	inline void write(int x) {
		if(x < 0) putchar('-'), x = -x;
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
		return;
	}
}
using namespace fastIO;
int n, ans, x;
string s;
bool judge(string str) { // 判断“很酷的单词”
	int ch[26] = {};
	int length = str.length();
	for (int i = 0; i < length; i ++) {
		ch[str[i] - 'a'] ++; // 记录出现次数
	}
	int s = 0;
	for (int i = 0; i < 26; i ++) {
		s += ch[i];
	}
	if (s < 2) return false; // 判断不同的字母出现次数是否不同
	for (int i = 0; i < 26; i ++) {
		for (int j = 0; j < 26; j ++) {
			if (j != i && ch[i] == ch[j] && ch[i] != 0) return false;
		}			
	}		
	return true;
}
int main() {
	while (cin >> n) {
		ans = 0; 
		x++;
		for (int i = 1; i <= n; i ++) {
			cin >> s;
			if (judge(s)) ans ++; // 判断 
		}
		cout << "Case " << x << ": " << ans << endl;//输出
	}
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值