//
// main.m
// 最大公共子字符串
//
// Created by SUN on 16/7/31.
// Copyright © 2016年 VitoSun. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str1 = @"zqyerwre";
NSString *str2 = @"zeqwse";
NSString *temp = [NSString string];
NSMutableArray *mArray = [NSMutableArray array];
//找出所有公共字符串
for (int i = 0; i<str1.length; i++) {
for (int k = 0; k<str1.length+1-i; k++) {
temp = [str1 substringWithRange:NSMakeRange(k, i)];
if([str2 containsString:temp]){
[mArray addObject:temp];
}
}
}
int i = (int)mArray.count-1;
NSString *max = mArray[i];
//从后向前,若长度不等,结束循环
while([mArray[i] length]==[mArray[i-1] length]){
if([mArray[i-1] compare:max]==NSOrderedDescending){
max = mArray[i-1];
}
if(--i ==0){
break;
}
}
NSLog(@"%@",mArray);
NSLog(@"%@",max);
}
return 0;
}