Compound Words UVA - 10391(字符串搜索)

Problem
You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
Solve
1.提高效率我们用 set 输入结束再一并处理
2.迭代处理,符合以下条件就输出
3.每个单词以所有可能分成两部分,分别匹配单词集,均存在即符合条件
Method

string.assign(string, (int)start, (int)length) 分配函数

int main() {//int T; cin >> T; getchar();
    ios::sync_with_stdio(false);
    set<string> ste;
    string s, t;
    while(cin >> s) ste.insert(s);
    for(set<string>::iterator it = ste.begin(); it != ste.end(); it++) {
        s = *it;
        for(int i = 1; i < s.length(); i++) {
            t.assign(s, 0, i);
            if(ste.count(t)) {
                t.assign(s, i, s.length()-i);
                if(ste.count(t))
                    {cout << s << endl; break;}
            }
        }
    }
    return 0;
}
发布了54 篇原创文章 · 获赞 43 · 访问量 1937

猜你喜欢

转载自blog.csdn.net/Jungle_st/article/details/104763174