排序 POJ 1318 Word Amalgamation

原题地址

Word Amalgamation

解题思路

可以对字符串直接用 sort() 函数排序,默认就是按照字典序来排的~

注意事项

一开始用了 auto it 就CE了…切记切记,声明一下 it 的类型。

参考代码

#include<stdio.h>
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<algorithm>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<stack>
//#include<bits/stdc++.h>
#define LOCAL  //提交的时候一定注释
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
typedef long long LL;
typedef double db;
const db pi = acos(-1);
using namespace std;
const int maxn = 110;

int readint() {
    
    
    int x; scanf("%d", &x); return x;
}

set<string> st;
vector<string> v, ans;
bool has[maxn], vis[10];
int main() {
    
    
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
//   freopen("output.txt", "w", stdout);
#endif
    int cnt = 0;
    string s;
    while (cin >> s) {
    
    
        if (s == "XXXXXX") {
    
    
            if (!cnt) {
    
    cnt++;}
            else break;
        } else {
    
    
            if (!cnt) {
    
    
                st.insert(s);
            } else {
    
    
                v.push_back(s);
            }
        }
    }
    for(int i = 0; i < v.size(); i++) {
    
    
        ans.clear();
        string now = v[i];
        sort(now.begin(), now.end());
        set<string> ::iterator it;
        for(it = st.begin(); it != st.end(); it++) {
    
    
            string tmp = *it;
            if (tmp.size() != now.size()) continue;
            sort(tmp.begin(), tmp.end());
            if (tmp == now) ans.push_back(*it);
        }
        for(int k = 0; k < ans.size(); k++) {
    
    
//            cout << it << endl;
            printf("%s\n", ans[k].c_str());
        }
        if (!ans.size()) printf("NOT A VALID WORD\n");
        printf("******\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Encore47/article/details/112983167