字符串環(字符串包含)


題目描述

有兩個字符構成的環。請寫一個程序,計算這兩個字符環上最長公共字符串的長度。例如,字符串“ABCEFAGADEGKABUVKLM”的首尾連在一起,構成一個環;字符串“MADJKLUVKL”的首尾連在一起,構成一個另一個環;“UVKLMA”是這兩個環的一個公共字符串。
 

輸入

若干行,每行包括兩個不包含空格的字符串。這兩個字符串用空格分開。若其中某個字符串的長度為1,則表示結束。否則,每個字符串的首尾相連即為一個環。每個環上字符總數不超過255。

輸出

為每行輸入,分別輸出一個整數,表示這兩個字符環上最長公共字符串的長度。最后一行沒有輸出。
 

樣例輸入

ABCEFA24*92(GADEGKABUVKLM  AD&30ijJKLAaUVKLM
313435t974  008bac
A 33

樣例輸出

6
0
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
 
int main (void) 
{
	int  t, max;
	string s1, s2, st;
	while(cin >> s1 >> s2) 
	{
		if(s1.length()==1 || s2.length()==1) 
		{
			break;
		}
		s1 = s1 + s1;
		s2 = s2 + s2;
		max = 0;
		while(s2.length() > 0) 
		{
			for(int i=1; i<=s2.length(); i++) 
			{
				t = s1.find(s2.substr(0, i));
				if(t>-1 && i>max)
				    max = i;
			}
		   s2.erase(0, 1);
		}
		cout << max << endl;
	}
	return 0;
}

  

//判斷一個字符串是否包含在另一個字符串之內(連續)
 
int main (void) 
{
	int  t, max;
	string s1, s2, st;
	while(cin >> s1 >> s2) 
	{
		if(s1.find(s2)==s1.npos)   //s1是否包含s2 
		   cout<<"NO"<<endl;
		else 
		    cout<<"YES"<<endl;
	}
	return 0;
}
//判斷一個字符串是否包含在另一個字符串之內(非連續)

#include <iostream>
#include <string>
#include <cstdio>
#include<regex>
using namespace std;
string a="happy";
int main ()
{
    string str;
     cin>>str;
    regex pattern(a);
    if(regex_match(str,pattern))
           cout<<"No!"<<endl;
     else
           cout<<"Yes!"<<endl;
      return 0;
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM