開啟章節選單

417 Word Index

程式碼

#include <bits/stdc++.h>
using namespace std;
//author:Jackis666
int main(){
        int dp[6][27]={0};
        for(int i=1;i<27;i++){
            dp[1][i]=dp[1][i-1]+1;
        }
        for(int i=2;i<6;i++){
            dp[i][0]=dp[i-1][26];
            for(int j=1;j<27;j++){
                dp[i][j]=dp[i][j-1]+dp[i-1][26]-dp[i-1][j];
            }
        }
        string s;
        while(cin>>s){
            int n=s.size();
            bool ok=false;
            for(int i=0;i<n-1;i++){
                if(s[i]>=s[i+1]){
                    cout<<"0\n";
                    ok=true;
                    break;
                }
            }
            if(ok) continue;
            int ans=0;
            for(int i=1;i<n;i++){
                ans+=dp[n-i][s[i]-'a']-dp[n-i][s[i-1]-'a'+1];
            }
            ans+=dp[n][s[0]-'a']+1;
            cout<<ans<<endl;
        }
}
// author: Piau
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define INF LONG_LONG_MAX/1000
#define WA() cin.tie(0)->sync_with_stdio(0)
#define all(x) (x).begin(), (x).end()
#define int long long
#define PII pair<int, int>

map<string, int> mp;
int idx = 0;

void gen(string &now, int len) {
    if (now.size() == len) return mp[now] = ++idx, void();
    for (char c = (now.empty() ? 'a' : now.back()+1); c <= 'z'; c++) {
        now += c;
        gen(now, len);
        now.pop_back();
    }
}

signed main() { WA();
    string s;
    for (int i = 1; i <= 5; i++) gen(s, i);
    while (cin >> s) cout << mp[s] << '\n';
}