爬格子呀5-5

今天算是比较渣了,一晚上只写了一道题,不过还是学到了蛮多东西的;
中途请教了一位学长,虽然有一点的坏,但是的确告诉了我一条挺好的解决方法;
现在是这样,要多去尝试一下新的方法,当自己真正用熟了,就可以为所欲为了;
现在是晚上0:40,希望自己的努力不会辜负自己的期望,晚安世界;

代码如下:(栈溢出版本/啰里啰唆版本)

#include<cstdio>
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
const int N = 120000;
int n;
using namespace std;

int main() {
    vector<string>word[N];
    string s, ss;
    cin >> n;
    int t = 0;
    while (t++ < n) {
        scanf_s("%s", &word[t]);
    }
    vector<string>::iterator it = word->begin(), itt, target;
    for (; it != word->end(); it++) {
        for (itt = it; itt != word->end(); itt++) {
            s = *it + *itt;
            target = find(word->begin(), word->end(), s);
            if (target != word->end()){
                cout << *target;
            }
        }
    }
    return 0;
}

新版代码如下,进行了总体的优化,算法复杂度,代码简洁度均得到了一定程度的提升;

代码如下:

#include<stdio.h>
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<set>
const int N = 120000;
int n;
using namespace std;

int main() {
    set<string> word;
    string s;
    cin >> n;
    int t = 0;
    while (t++ < n) {
        cin >> s;
        word.insert(s);
    }
    set<string>::iterator itt;
    for (set<string>::iterator i = word.begin(); i != word.end(); i++) {
        itt = (++i)--;
        s = *itt + *i;
        if (word.count(s)) {
            cout << s<<endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/romaLzhih/p/9489861.html