这道题其实就是按照公式模拟。
首先给出一个句子,然后要分成一个一个的单词,每个单词都有值。
单词值的计算方法如题目中的 : bz 就是 2 × 32 + 26 那么 abz 就是1 ×32×32 + 2 × 32 +26;也就是a对应1,然后按32进制转化。
然后意思就是用找出一个C 用这个C来套公式,使得每个单词算出来的结果不一样。
n代表有n个单词;
集合W中有n个元素,是每个单词对应的值,已经从小到大排列了;
C必须尽量小,而且C一定是W中某个元素的倍数,所以C一开始的值就是w1。
然后C的变化题目中也已经给出,如果这个C使得
不满足,那么下一个试的C就应该是
AC代码:
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int N = 1000;
string str;
int num[N];
int n,c;
int mi (int a ,int b) {
return a < b ? a : b;
}
void solve() {
for (int i = 0 ; i < n ;i++) {
for (int j = i + 1 ; j < n ;j++) {
if ( ((c / num[i]) % n) == ((c / num[j]) % n)){
c = mi(((c / num[i]) + 1) * num[i], ((c / num[j]) + 1) * num[j]);
solve();
return ;
}
}
}
}
int main () {
while (getline(cin ,str)) {
n = 0;
memset(num ,0 ,sizeof(num));
for (int i = 0 ; i < str.size() ;i++) {
if (str[i] == ' ' && str[i - 1] != ' ') {
n++;
continue;
}
if (str[i] == ' ' && str[i - 1] == ' ') {
continue;
}
num[n] = num[n] * 32 + str[i] - 'a' + 1;
}
n += 1;
sort(num , num + n);
// cout << n <<endl;
// for (int i = 0 ; i < n ;i++) {
// cout <<num[i] << endl;
// }
c = num[0];
solve();
cout <<str << endl;
cout << c << endl << endl;
}
}