qoj4804 Regular Expression

题意

给出一个字符串,找到匹配字符串的正则表达式的最小长度和数量。

思路

请输入文本。。。

符号 . 能匹配任何字符,* 可以匹配任意多次,+ 可以匹配一次以上,因此 .* 能匹配任意字符串,.+ 能匹配任意非空字符串。

因此可以证明答案的最小长度不会超过 \(2\)

特判字符串长度为 \(1,2\) 的情况,其他直接输出。

注意当字符全部相同时 x*x+x 为相同的字符)也是可以匹配的。

代码

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int T;
string s;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	cin>>T;
	while(T--){
		cin>>s;
		if(s.length()>2){
			bool sm=1;
			for(int i=1;i<s.length();i++)
				if(s[i]!=s[i-1]) sm=0;
			if(sm) cout<<"2 4"<<endl;
			else cout<<"2 2"<<endl;
		}
		else if(s.length()==2){
			if(s[0]!=s[1]) cout<<"2 6"<<endl;
			else cout<<"2 8"<<endl;
		}
		else cout<<"1 2"<<endl;
	}
	return 0; 
}
posted @ 2025-09-02 15:13  WuMin4  阅读(19)  评论(0)    收藏  举报