思路没问题,样例也过了,为什么全wa
查看原帖
思路没问题,样例也过了,为什么全wa
972985
jirun楼主2025/6/25 17:43

先对输入的字符串进行判断,然后掐头去尾(bushi),再用sort将头尾排序,最后进行比较,为什么会全Wa

#include<bits/stdc++.h>
using namespace std;
int T,n;
bool check(string s){
	int y=s.length();
	for(int i=1;i<y;i++){
		if(s[i-1]>s[i])return 1;
	}
	return 0;
}
int main(){
	cin>>T;
	for(int i=1;i<=T;i++){
		string a[101];
		cin>>n;
		bool che=1;
		for(int j=1;j<=n;j++){
			cin>>a[j];
			if(check(a[j])) {
				che=0;
				cout<<0<<endl;
				break;
			}
		}
		if(che==0) continue;
		if(n==1){
			cout<<1<<endl;
			continue;
		}
		int fst[n+1],lst[n+1];
		for(int j=1;j<=n;j++){
			fst[j]=a[j][0];
			lst[j]=a[j][a[j].length()-1];
		}
		sort(fst+1,fst+n+1);
		sort(lst+1,lst+n+1);
		for(int j=2;j<=n;j++){
			if(lst[j-1]>fst[j]){
				che=0;
				cout<<0<<endl;
				break;
			}
		}
		if(che==0) continue;
		cout<<1<<endl;
	}
	return 0;
}
2025/6/25 17:43
加载中...