質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%

Q&A

解決済

1回答

729閲覧

競技プログラミングで、テンプレートで答えを出力した時WAが出てしまう。また言語はc++14 です。

donkey

総合スコア24

0グッド

0クリップ

投稿2020/02/21 16:57

編集2020/02/21 17:02

######解いた問題はATcorder上の『細長いお菓子』と言う問題です。下にリンクを貼っておきます。

リンク内容
↓出力に関する制限だけ抜き出しておきます。
『高橋君がこのお菓子から切り出すことの出来る「同じ味のブロックが2 つ以上含まれない、ひと繋がりになった部分」の最大の長さをcm単位で表す1つの整数を1行に出力せよ。出力の末尾に改行をいれること。』

#####これから2つのコードを掲示します。前者はACであり、後者がWAです。二つの違いは入出力をテンプレートで行なっているか否かです。

c++

1//accepted code 2#include <bits/stdc++.h> 3#include <algorithm> 4#include <cmath> 5#include <set> 6#include <cstdio> 7#include <vector> 8#include <iostream> 9#include <utility> 10#include <queue> 11#include <map> 12 13#define fir first 14#define sec second 15#define sz(s) (s).size() 16#define pb push_back 17#define get(n) scanf("%d",&n); 18#define gets(s) string s;cin >> (s); 19#define prfi(n) printf("%d", &n); 20#define prfd(n) printf("%lf", &n); 21#define All(s) (s).begin(), (s).end() 22#define rep(i,j) for(int (i)=0;(i)<(j);(i)++) 23#define For(i,j,k) for(int (i)=(j);(i)<(k);(i)++) 24#define repd(i,j) for(int (i)=(j);(i)>=0;(i)--) 25#define Ford(i,j,k) for(int (i)=(j);i>=(k);i--) 26#define vfor(c,v) for(auto (c): v) 27#define dump(x) std::cout << #x << " = " << (x) << std::endl; 28#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; 29 30using ll = long long; 31using pii = std::pair<int,int>; 32using vi = std::vector<int> ; 33using vvi = std::vector<vi> ; 34using vll = std::vector<ll>; 35using vvll = std::vector<vll>; 36using vd = std::vector<double> ; 37using vvd = std::vector<vd> ; 38using qi = std::queue<int> ; 39using vpii = std::vector<std::pair<int, int> >; 40using namespace std; 41 42const int Mod = (1e9) + 7; 43const int max_n = 3 * (1e5) + 1; 44const int max_m = 83 * (1e5) + 1; 45const int INF = 10241024; 46const ll INFL = (1ll << 62) - 1; 47const int dx[] = {-1, 0, 0, 1}; 48const int dy[] = {0, -1, 1, 0}; 49//_____________________________________Templates_________________________________________// 50 51template<class T1, class T2> inline void chmin(T1 &a, T2 b){if(a > b) a = b;} 52template<class T1, class T2> inline void chmax(T1 &a, T2 b){if(a < b) a = b;} 53 54//mainly use for dynamic prog 55template<class T1, class T2> 56void update(T1 &a, T2 b){ 57 a += b; 58 if(a > Mod) a %= Mod; 59} 60 61inline void IN(void){ 62 return; 63} 64 65template <typename First, typename... Rest> 66void IN(First& first, Rest&... rest){ 67 cin >> first; 68 IN(rest...); 69 return; 70} 71 72inline void OUT(void){ 73 cout << "\n"; 74 return; 75} 76 77template <typename First, typename... Rest> 78void OUT(First first, Rest... rest){ 79 cout << first << " "; 80 OUT(rest...); 81 return; 82} 83//_____________________ following sorce code_________________________// 84int n,m,k; 85//vvll dp(400, vll(400)); 86string S; 87vll cost(1 << 16); 88int a,b,c; 89int h,w; 90vi v; 91vll u; 92 93bool check(){ 94} 95 96int main () { 97 cin.tie(0); 98 ios::sync_with_stdio(false); 99 IN(n); 100 v = vi(n); 101 rep(i,n)IN(v[i]); 102 int ans = 0; 103 int idx = 0; 104 map<int, int> mp; 105 rep(i,n){ 106 while(idx < n && mp[v[idx]] == 0){ 107 mp[v[idx]]++; 108 idx++; 109 } 110 111 chmax(ans, idx - i); 112 if(i == idx)idx++; 113 else mp[v[i]]--; 114 } 115 cout << ans << endl; 116 //cout << fixed << setprecision(15) << ans << endl; 117 return 0; 118}

次にWAのコードを掲示します。

#include <bits/stdc++.h> #include <algorithm> #include <cmath> #include <set> #include <cstdio> #include <vector> #include <iostream> #include <utility> #include <queue> #include <map> #define fir first #define sec second #define sz(s) (s).size() #define pb push_back #define get(n) scanf("%d",&n); #define gets(s) string s;cin >> (s); #define prfi(n) printf("%d", &n); #define prfd(n) printf("%lf", &n); #define All(s) (s).begin(), (s).end() #define rep(i,j) for(int (i)=0;(i)<(j);(i)++) #define For(i,j,k) for(int (i)=(j);(i)<(k);(i)++) #define repd(i,j) for(int (i)=(j);(i)>=0;(i)--) #define Ford(i,j,k) for(int (i)=(j);i>=(k);i--) #define vfor(c,v) for(auto (c): v) #define dump(x) std::cout << #x << " = " << (x) << std::endl; #define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; using ll = long long; using pii = std::pair<int,int>; using vi = std::vector<int> ; using vvi = std::vector<vi> ; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vd = std::vector<double> ; using vvd = std::vector<vd> ; using qi = std::queue<int> ; using vpii = std::vector<std::pair<int, int> >; using namespace std; const int Mod = (1e9) + 7; const int max_n = 3 * (1e5) + 1; const int max_m = 83 * (1e5) + 1; const int INF = 10241024; const ll INFL = (1ll << 62) - 1; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; //_____________________________________Templates_________________________________________// template<class T1, class T2> inline void chmin(T1 &a, T2 b){if(a > b) a = b;} template<class T1, class T2> inline void chmax(T1 &a, T2 b){if(a < b) a = b;} //mainly use for dynamic prog template<class T1, class T2> void update(T1 &a, T2 b){ a += b; if(a > Mod) a %= Mod; } inline void IN(void){ return; } template <typename First, typename... Rest> void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return; } inline void OUT(void){ cout << "\n"; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest){ cout << first << " "; OUT(rest...); return; } //_____________________ following sorce code_________________________// int n,m,k; //vvll dp(400, vll(400)); string S; vll cost(1 << 16); int a,b,c; int h,w; vi v; vll u; bool check(){ } int main () { cin.tie(0); ios::sync_with_stdio(false); IN(n); v = vi(n); rep(i,n)IN(v[i]); int ans = 0; int idx = 0; map<int, int> mp; rep(i,n){ while(idx < n && mp[v[idx]] == 0){ mp[v[idx]]++; idx++; } chmax(ans, idx - i); if(i == idx)idx++; else mp[v[i]]--; } OUT(ans); //cout << fixed << setprecision(15) << ans << endl; return 0; }

アルゴリズムに関しては違いは無く、話題とする必要は感じませんので、省略します。

######何故後者のコードがWAになっているか理解出来ません。何卒ご教授下さい。c++14を用いております。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

行末にスペースが出力されるからでしょう。

投稿2020/02/21 20:02

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

donkey

2020/02/21 23:41

おっしゃる通りで。スペース消したら通りました。最大の謎みたいに考えていた自分が恥ずかしい限りです。 それは兎も角として、解決して嬉しいです。ご指摘有難うございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問