NOIP2011-J 统计单词数

一道普及组菜题竟然卡了我大概2个月
我太菜了,我太失败了
今天骤然A掉
笔者很以为有写一点东西的必要

字符串真的不是特别好处理
而且这道题要注意的点又特别多

在csp前一天A掉这道题,也希望明天能超常发挥吧,毕竟我真的太菜了/ll/ll/ll
最近整体思想的训练比较多,zgs的代码也终于能看懂了

整理一下对这道题的思考历程

  1. 用string,一个点都A不了,毕竟我不太能用string驾驭住这道题
  2. 极不愿意的改成数组
    然鹅只能A 3个——我并没有认真审题
    小心啊!!!明天(重点~ 同学门~ 倒装句哦~)
#include<bits/stdc++.h>
using namespace std;
#define in Read()
#define re register
char word[20],txt[(int)1e6+10];
int w,t;
int sum;

inline bool Calc(int p){
	for(re int i=p;i<p+w;i++)
		if(txt[i]!=word[i-p])
			return false;
	return true;
}

int main(){
	scanf("%s",word);
	w=strlen(word);
	for(re int i=0;i<w;i++)word[i]=tolower(word[i]);
	gets(txt);gets(txt);
	t=strlen(txt);
	for(re int i=0;i<t;i++)txt[i]=tolower(txt[i]);
	
	int first=0;bool v=true,c;
	for(re int i=0;i<t;i++)
		if(txt[i]==word[0]){
			c=Calc(i);
			sum+=c;
			if(c&&v){
				v=false;
				first=i;
			}
		}
			
	
	printf("%d %d",sum,first);
	
	return 0;
}
  1. 改了一下把没有单词考虑进去了
    现在能A 4个
#include<bits/stdc++.h>
using namespace std;
#define in Read()
#define re register
char word[20],txt[(int)1e6+10];
int w,t;
int sum;

inline bool Calc(int p){
	for(re int i=p;i<p+w;i++)
		if(txt[i]!=word[i-p])
			return false;
	return true;
}

int main(){
	scanf("%s",word);
	w=strlen(word);
	for(re int i=0;i<w;i++)word[i]=tolower(word[i]);
	gets(txt);gets(txt);
	t=strlen(txt);
	for(re int i=0;i<t;i++)txt[i]=tolower(txt[i]);
	
	int first=0;bool v=true,c;
	for(re int i=0;i<t;i++)
		if(txt[i]==word[0]){
			c=Calc(i);
			sum+=c;
			if(c&&v){
				v=false;
				first=i;
			}
		}
			
	
	if(sum)printf("%d %d",sum,first);
	else printf("-1");
	
	return 0;
}
  1. 考虑空格的时候很头疼,要防止溢出但是总是搞不好
    这就是我很讨厌字符串的地方
    就在我马上就要妥协使用很丑陋的分离读单词的方法的时候
    我灵机一动——为什么不 g e t s ( t x t + 1 ) gets(txt+1) 呢!!!
    最后在注意一下:
    ‘ ’的码是32,不是0!!!
    我又在这个地方卡了很久/ll/ll/ll

终于切掉这道题了


#include<bits/stdc++.h>
using namespace std;
#define in Read()
#define re register
char word[20],txt[(int)1e6+10];
int w,t;
int sum;

inline bool Calc(int p){
	for(re int i=p;i<p+w;i++)
		if(txt[i]!=word[i-p+1])
			return false;
	if(txt[p+w]==' ')return true;
	else return false;
}

int main(){
	scanf("%s",word+1);
	w=strlen(word+1);
	for(re int i=1;i<=w;i++)word[i]=tolower(word[i]);
	gets(txt);gets(txt+1);
	t=strlen(txt+1);
	for(re int i=1;i<=t;i++)txt[i]=tolower(txt[i]);
	
	int first=0;bool v=true,c;
	for(re int i=1;i<=t;i++)
		if(txt[i]==word[1]&&txt[i-1]==' '){
			c=Calc(i);
			sum+=c;
			if(c&&v){
				v=false;
				first=i;
			}
		}
			
	
	if(sum)printf("%d %d\n",sum,first-1);
	else printf("-1\n");
	
//	for(re int i=1;i<=w;i++)printf("%c",word[i]);
//	puts("");
//	for(re int i=1;i<=t;i++)printf("%c",txt[i]);
	return 0;
}
发布了26 篇原创文章 · 获赞 3 · 访问量 907

猜你喜欢

转载自blog.csdn.net/Antimonysbguy/article/details/103092194