公共字符串个数
来源:互联网 发布:不满意淘宝投诉结果 编辑:程序博客网 时间:2024/05/03 21:30
输入过程中getline会认为是一个字符串,所以利用string中的find()函数找到第一次出现空格,并用assign()进行复制计算,分成两个string。
string s,s1,s2; getline(cin,s); int len=s.length(); int k=s.find_first_of(' ',0); s1.assign(s,0,k); s2.assign(s,k+1,len);//分成两个s1,s2
计算两个字符串的最大公共子串(Longest Common Substring)的长度,字符不区分大小写。
第一种方法,直接进行比较和循环嵌套,由于是不区分大小写,大小写之间的差值是32.
#include <iostream>#include <string>using namespace std;int getCommonStrLength( char *pFirstStr, char *pSecondStr){int i,max; char *binF,*endF,*binS,*endS; max=0; for (binF=pFirstStr;*binF!='\0';binF++) { endF=binF; for (binS=pSecondStr;*binS!='\0';binS++) { endS=binS; i=0; while(*endF!='\0' && *endF!='\0'&& (*endF==*endS ||*endF==(*endS+'A'-'a') ||*endS==(*endF+'A'-'a'))) { i++; endF++; endS++; } if(max<=i) max=i; }}return max;}int main(){char s1[100],s2[100];cin>>s1;cin>>s2;int result=getCommonStrLength( s1, s2);cout<<result<<endl;system("pause");return 0;}
是数字最大的就是公共字符串的个数。用的是vector矢量。
#include <iostream>#include <sstream>#include <string>#include <vector>using namespace std;int getCommonStrLength(string pFirstStr, string pSecondStr){if( (pFirstStr==" ")|| (pSecondStr==" "))return 0;int m=pFirstStr.size();int n=pSecondStr.size();vector<vector<int> > table(m+1,vector<int>(n+1));int max=0;for(int i=0;i<m+1;++i){for(int j=0;j<n+1;++j){if(i==0||j==0)table[i][j]=0;else if(pFirstStr[i-1]==pSecondStr[j-1]){table[i][j]=table[i-1][j-1]+1;if(table[i][j]>max)max=table[i][j];}elsetable[i][j]=0;}}return max;}int main(){string s,s1,s2;getline(cin,s);int len=s.length();int k=s.find_first_of(' ',0);s1.assign(s,0,k);s2.assign(s,k+1,len);int result=getCommonStrLength( s1, s2);cout<<result<<endl;system("pause");return 0;}
0 0
- 公共字符串个数
- POJ 3415 求两个字符串间长度不小于k的公共子串的个数
- 关于公共子串的分析,求两个字符串中所有公共子串个数,以及最长公共子串和长度。
- 字符串个数
- 字符串个数
- 公共子序列的个数
- 求公共前缀长度与所选字符串个数的乘积的最大值 Trie树求最值 UVA 11488 Hyper Prefix Sets
- poj 3294 求多于k个字符串的最长公共子串的个数-------后缀数组+二分答案
- 最长公共子字符串
- 最长公共子字符串
- 字符串公共串
- 最长公共子字符串
- 最长公共子字符串
- 公共字符串strstr
- 查找字符串公共后缀
- 最长公共子字符串
- 公共字符串计算
- 求最大公共字符串
- iOS-学习笔记-UI-第十九天
- 4.17~4.22
- leetcode[112]:Path Sum
- 黑马程序员---java反射机制的实现原理
- ARM异常处理
- 公共字符串个数
- 进阶第21课时,实践,简易银行系统开发第四版
- XRDP与VNC的关系
- GNU ARM伪指令
- 黑马程序员---工厂设计模式
- UVA10892 LCM Cardinality(gcd)
- framework制作
- 算术移位和逻辑移位
- LeetCode_Linked List_Remove Duplicates from Sorted List II