zoj 1170

该题的主要思路是,在两个字符串中固定其中一个字符串,移动另外一个字符串进行比较,得到其中相同字符数的最大值
然后在固定两外一个,重复上述步骤
#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
         using namespace std; string Str, Ptr; int Match(int i, int length, int j, int count) { int max = 0; while (i < length&&j < count) { if (Str[i] == Ptr[j]) { max++; } i++; j++; } return max; } int gcd(int a, int b) { int g; if (b == 0) g = a; else g = gcd(b, a%b); return g; } int main(int argc, char argv[]) { //ifstream cin("F:\\ac.txt"); char str[100]; while (cin.getline(str, 100)) { if (strcmp(str, "-1") == 0) break; //首先对字符串数组进行处理,分成两个字符串 int i; int length = strlen(str); for (i = 0; i < length; i++) { if (str[i] != ' ') Str += str[i]; else { i = i + 1; break; } } for (; i < length; i++) { Ptr += str[i]; } int count = Str.length(); int number = Ptr.length(); int max = 0; for (int j = 0; j < count; j++) { //第二个字符串位置不动,移动第一个字符串,查找相同字符的个数 int res = Match(j, count, 0, number); if (max < res) max = res; } for (int j = 0; j < count; j++) { //第一个字符串数组位置不懂,移动第二个字符串进行查找 int res = Match(0, count, j, number); if (max < res) max = res; } max = max * 2; int Gcd = gcd(max, count + number); if (max == 0) { cout << "appx(" << Str << "," << Ptr << ") = 0" << endl; } else if (max == count + number) { cout << "appx(" << Str << "," << Ptr << ") = " << (count + number) / Gcd << endl; } else { cout << "appx(" << Str << "," << Ptr << ") = " << max / Gcd << "/" << (count + number) / Gcd << endl; } /*cout << max << endl; cout << Str << "," << Ptr << endl;*/ Str.clear(); Ptr.clear(); } return 0; } 
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值