#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int cnt = 0;
for (int i = 0; i < n; i++)
{
string a;
cin >> a;
vector<int> check(26, 0);
check[a[0] - 'a'] = 1;
for (int j = 1; j < a.size(); j++)
{
if (a[j] != a[j - 1])
{
if (check[a[j] - 'a'] != 0)
{
cnt++;
break;
}
else
{
check[a[j] - 'a'] = 1;
}
}
}
}
cout << n - cnt;
return 0;
}
<aside> 💡
문자열 a를 입력받고 문자열의 각 자리를 돌면서 문자열에 등장하는 알파벳에 1로 체크를 해준다. 문자열 a의 다음 자리가 그 전의 자리 알파벳과 다른 경우 그 알파벳이 등장했었는지 확인하고 등장했었다면 그룹 단어가 아니므로 그룹 단어가 아닌 단어 개수인 변수 cnt를 증가시킨다. 만약 등장하지 않았던 알파벳이라면 1로 체크를 하고 넘어간다. 출력은 n-cnt로 그룹 단어의 개수를 출력한다.
</aside>
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
int x1, x2, y1, y2;
vector<int> answer(m);
vector<vector<int>> matrix(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> matrix[i][j];
}
}
vector<vector<int>> sum(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + matrix[i][j];
}
}
for (int i = 0; i < m; i++)
{
cin >> x1 >> y1 >> x2 >> y2;
answer[i] = sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1];
}
for (int i = 0; i < m; i++)
{
cout << answer[i] << "\\n";
}
return 0;
}
<aside> 💡
행렬 matrix를 입력받고 구간합 행렬 sum을 새로 만든다. answer을 구할 때 주의해야 하는 점은 두번 제거되는 부분은 한번 더해줘야 한다는 것이다.
</aside>