この
トピックは少し嫌です。トピックは投稿されています:
トピック:
ターゲット値と文字列を指定します最初に、次の条件を制約します
- 26個の大文字に対応する値は(A-1、B-2、C-3 ...)です。
- 指定された文字列で5つの一意の文字を見つけて、長さ5の文字列を形成します。文字列の左から右への対応する値は、a、b、c、d、eです。
- a-b ^ 2 + c ^ 3-c ^ 4 + d ^ 5 =ターゲットを満たす
- このように長さ5の文字列が複数ある場合は、辞書式順序が最大の文字列を出力し、そうでない場合は「解決策なし」を出力します。
トピックのアイデア
- まず、最初の4文字を激しく列挙し、5番目の文字が指定された文字列に表示されるかどうかを確認します。表示される場合はベクトルに保存され、表示されない場合は文字列は考慮されません。
- カスタムcmp関数を使用して文字列を昇順で並べ替えます
- math.hライブラリのpow関数は問題がある可能性があるため、数値の累乗を計算する関数も自分で手書きします
- コードはもう少し面倒で、タイトルの意味を理解するのは難しくありません
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef __int64 bi;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){
return x&(-x);}
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){
if(!b){
d=a,x=1,y=0;}else{
ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){
ll res=1;a%=MOD;while(b>0){
if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){
return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){
ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){
ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {
if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = {
{
1,0}, {
-1,0},{
0,1},{
0,-1} };
map<char,int> m; //保存约束值
vector<string> st; // 保存每次符合条件的字符串
string s; // 题目给定的字符串
int len; // 题目给定字符串的长度
// 初始化题目给定的条件约束
void init(){
for(char i='A';i<='Z';++i)
m[i] = i - 'A' + 1;
}
// 求数的幂次方值
int calPow(int b,int p){
int sum = 1;
for(int i=1;i<=p;++i)
sum *= b;
return sum;
}
// 判断这四个固定的字母是否符合
char judge(int a,int b,int c,int d,const int &ans){
set<int> jk{
a,b,c,d};
if(jk.size() < 4) return ' ';
int mid = ans - (calPow(a,5)-calPow(b,4)+calPow(c,3)-calPow(d,2));
if(mid==a || mid == b || mid == c || mid == d) return ' ';
for(auto i:s){
if(m[i] == mid)
return i;
}
return ' ';
}
// 拼接五个字母为字符串
string binationString(char a,char b,char c,char d,char e){
string k = "";
k+=a;k+=b;k+=c;k+=d;k+=e;
return k;
}
// 按字典升序排列
bool cmp(const string &a,const string &b){
return a+b < b+a;
}
signed main(void)
{
int a;
init();
while(cin>>a>>s){
if(a==0 && s == "END") break;
len = s.length();
st.clear();
for(int i=len-1;i>=0;--i){
for(int j=len-1;j>=0;--j){
for(int k=len-1;k>=0;--k){
for(int z=len-1;z>=0;--z){
char res = judge(m[s[i]],m[s[j]],m[s[k]],m[s[z]],a);
if(res != ' '){
st.push_back(binationString(res,s[z],s[k],s[j],s[i]));
}
}
}
}
sort(st.begin(),st.end(),cmp);
if(st.size() == 0) cout <<"no solution"<<endl;
else cout<<*st.rbegin()<<endl; // 逆序迭代器输出字典排序最大的那个字符串
}
}