UVA10252 POJ2629 Common Permutation【字符串排序】

问题链接:UVA10252 POJ2629 Common Permutation

问题描述参见上文。两个小写字母构成的字符串a和b,求各自的置换的最长公共子串,按字母顺序输出。

问题分析:(略)。

程序说明:字符串类型变量的排序也是可以用函数sort()实现的。

AC的C++语言程序如下:

/* UVA10252 POJ2629 Common Permutation */

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>

using namespace std;

int main()
{
    string a, b;
    int lena, lenb;

    while(getline(cin, a) && getline(cin, b)) {
        // 计算字符串长度
        lena = a.size();
        lenb = b.size();

        // 字符串排序
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());

        // 匹配求公共子串,输出结果
        for(int i=0, j=0; i<lena && j<lenb; ) {
            if(a[i] == b[j]) {
                printf("%c", a[i]);
                i++, j++;
            } else if(a[i] > b[j])
                j++;
            else if(a[i] < b[j])
                i++;
        }
        printf("
");
    }

    return 0;
}


原文地址:https://www.cnblogs.com/tigerisland/p/7564050.html