誰能把b001這道題的程式碼解鎖給我參考一下呢?我寫的老是WA,但我仔細檢查了很多遍也沒找出是哪裡的問題?有興趣的可以看看我寫的程式碼:
#include <iostream>
#include <string>
using namespace std;
int min(int a,int b);
string mySubStr(string str,int start,int len);
int main()
{
int k;
string str;
while(cin >> k >> str)
{
int count = 0;
string temp;
bool equal;
for(int i = 1;i < str.length() - k + 1;i++)
{
temp = str.substr(i,k);
equal = true;
if(str.length() < k + 2)
{
cout << "Input Error!\n";
continue;
}
if(k != 1)
{
for(int j = 0;j < temp.length();j++)
{
if(j < temp.length() - 1)
{
if(temp[j] != temp[j + 1])
{
equal = false;
break;
}
}
else if(j == temp.length() - 1)
{
if(temp[j] != temp[j - 1])
{
equal = false;
break;
}
}
}
}
if(equal)
{
for(int t = 1;t <= min(i,(str.length() - i - k));t++)
{
if(mySubStr(str,i - 1,t) != str.substr(i + k,t))
{
continue;
}
count++;
}
}
}
cout << count << endl;
}
return 0;
}
int min(int a,int b)
{
return((a <= b)?a:b);
}
string mySubStr(string str,int start,int len)
{
string temp;
for(int i = start - len + 1;i <= start && temp.length() <= len;i++)
{
temp += str[i];
}
return temp;
}
#include<iostream>
#include<string>
using namespace std;
string s;
int k;
int SOL(int a,int b)
{
int l1,l2,len;
l1=a;
len=(b-a+1-k)/2;
l2=a+len-1+k+1;
if(s.substr(l1,len)==s.substr(l2,len))
return 1;
else return 0;
}
int main()
{
int ans,i,j;
while(cin>>k>>s)
{
ans=0;
for(i=0;i<s.length();i++)
for(j=i+k;j<s.length();j++)
{
if((j-i+1-k)%2==1) continue;
if(SOL(i,j)) ans++;
}
cout<<ans<<endl;
}
return 0;
}
回楼上的 对台湾人没什么印象