POJ 1458

测评机输入相当于手动输入,不是文本输入,结尾没有EOF

最开始做法是while(cin >> str) 然后将其加入数组中,但是这样不会在文末停止,输入变成了死循环。必须输入一组数据,解决一组数据

#include <iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
int main()
{
   //freopen("123.txt","r",stdin);
    int dp[1000][1000];
    string str1,str2;
    while(cin >> str1){
        cin >> str2;
        memset(dp,0,sizeof(dp));
        for(int p= 1; p <= str1.size();p++){
            for(int q = 1; q <= str2.size();q++){
                if(str1[p - 1] == str2[q - 1])  dp[p][q] = dp[p-1][q-1] +1;
                else{
                    dp[p][q] = max(dp[p-1][q],dp[p][q-1]);
                }
            }
        }
        cout << dp[str1.size()][str2.size()] << endl;
        str1.clear();
        str2.clear();


    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/ehdhg13455/article/details/81428019
今日推荐